Ishant Rana
Ishant Rana

Reputation: 9

how to save state of a fragment on moving to other fragment?

Navigation Drawer have 4 fragment A,B,C,D On moving from A to B want to save the list maintain in A.

onDestroyView is called but unable to pass arguments in that.

Upvotes: 0

Views: 320

Answers (2)

Rahul Chaudhary
Rahul Chaudhary

Reputation: 1061

Save instant of fragment by override onSaveInstanceState of fragment and restore on onActivityCreated, follow this

@Override
public void onSaveInstanceState(Bundle outState) {
    //Save the fragment's state here 
    super.onSaveInstanceState(outState);

}

and

@Override
public void onActivityCreated(Bundle savedInstanceState) {

    super.onActivityCreated(savedInstanceState);

    if (savedInstanceState != null) {
        //Restore the fragment's state here
    }
}

Upvotes: 1

Ashish Agrawal
Ashish Agrawal

Reputation: 1977

you can use

fragmentTransaction.addToBackStack(tag);

it will save each

 getSupportFragmentManager().popBackStack();

Upvotes: 0

Related Questions