How can I recreate a fragment?

I use the SwipeRefreshLayout widget to update my fragment when someone clicks on this view.

To recreate the action I should use:

SwipeRefreshLayout mSwipeRefreshLayout;

public static LobbyFragment newInstance() {
    return new LobbyFragment();
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_lobby, container, false);


    receiver = new MySQLReceiver();

    rlLoading = (RelativeLayout) view.findViewById(R.id.rlLoading);
    gvLobby = (GridView) view.findViewById(R.id.gvLobby);

    updateList();

    mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.mSwipeRefreshLayout);
    mSwipeRefreshLayout.setColorSchemeResources(R.color.pDarkGreen, R.color.pDarskSlowGreen, R.color.pLightGreen, R.color.pFullLightGreen);
    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {

            getActivity().recreate();
        }


    });


    return view;

}

But I do not want to recreate all the activity containing the presentation pager, I would like to recreate the fragment. How can i do this?

+4
source share
1 answer

You can use:

getSupportFragmentManager().beginTransaction()
                .replace(R.id.container, LobbyFragment.newInstance()).commit();

To recreate a fragment

Note. getSupportFragmentManager()- if you use the support fragment and AppCompatActivity, if you use the frame frame class, you need to usegetFragmentManager()

+7
source

Source: https://habr.com/ru/post/1653459/


All Articles