Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 858
Rúben Dias Avatar asked Sep 02 '16 16:09

Rúben Dias


People also ask

How to recreate same fragment in Android?

Show activity on this post. Re-attach a fragment after it had previously been deatched from the UI with detach(Fragment). This causes its view hierarchy to be re-created, attached to the UI, and displayed. Detach the given fragment from the UI.

How do you refresh a fragment on a resume?

The onResume() get called always before the fragment is displayed. Even when the fragment is created for the first time . So you can simply move your from onViewCreated() to onResume .

How do you reattach a fragment?

when your activity is recreated, the fragment gets destroyed. So you have to create new instance of fragment and add it again. Ok, but you cant retain the fragment view. If you want to retain the data which is in fragment, then use onSaveInstanceState() and onRestoreInstanceState() .

How do I start a new fragment?

Use replace() to replace an existing fragment in a container with an instance of a new fragment class that you provide. Calling replace() is equivalent to calling remove() with a fragment in a container and adding a new fragment to that same container. transaction. commit();


3 Answers

I'm using .detach() and .attach() for recreating the fragment.

ATTACH

Re-attach a fragment after it had previously been deatched from the UI with detach(Fragment). This causes its view hierarchy to be re-created, attached to the UI, and displayed.

DETACH

Detach the given fragment from the UI. This is the same state as when it is put on the back stack: the fragment is removed from the UI, however its state is still being actively managed by the fragment manager. When going into this state its view hierarchy is destroyed.

HOW I USE IT

getFragmentManager()         .beginTransaction()         .detach(LobbyFragment.this)         .attach(LobbyFragment.this)         .commit(); 
like image 167
G. Ciardini Avatar answered Sep 28 '22 12:09

G. Ciardini


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

like image 45
JAAD Avatar answered Sep 28 '22 11:09

JAAD


If you're using Navigation Component, you can use this:

findNavController().navigate(
    R.id.current_dest,
    arguments,
    NavOptions.Builder()
        .setPopUpTo(R.id.current_dest, true)
        .build()
)

This lets NavController pop up the current fragment and then navigate to itself. You get a new Fragment and fragment ViewModel also gets recreated.

like image 43
Dewey Reed Avatar answered Sep 28 '22 11:09

Dewey Reed