Rúben Dias Rúben Dias - 8 months ago 70
Android Question

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();

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);


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() {
public void onRefresh() {



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?


You can use :

                .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()