How can I recreate a fragment?

I’m using a widget called SwipeRefreshLayout, to refresh my fragment when someone pushes the view.

To recreate the activity I have to 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 don’t want to recreate the full activity that contains the view pager, I would like to recreate the fragment. How can I do that?

Answer

You can use :

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

To recreate your fragment

Note:getSupportFragmentManager() is if you are using support fragment and AppCompatActivity , if you are using framework fragment class you need to use getFragmentManager()