Devrath
Devrath

Reputation: 42824

onSaveInstanceState of previous fragment is called on-orientation of current fragment

What i am doing ::

i use the code

@Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putInt("yourSpinner", spnSearchByCity.getSelectedItemPosition());

    }

i use the code

fragment = FrgMdMap.newInstance(messengerObj);

                    if (fragment != null) {
                        getFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).addToBackStack(null).commit();
                    }

Questions::

Upvotes: 3

Views: 380

Answers (1)

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

How is this taking place ?

Actually Fragment-A is still alive and well underneath your Fragment-B that is due to the addToBackStack(null) added when you replaced the fragment.

How can i make sure this wont happen ?

You can remove the addToBackStack(null) or count the number of stacked fragments in your onSaveInstanceState method and when it is 0 then you can run the code inside your onSaveInstanceState

EDIT:

public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    FragmentManager fm = getActivity().getSupportFragmentManager();
    if(fm.getBackStackEntryCount() == 0)
    outState.putInt("yourSpinner", spnSearchByCity.getSelectedItemPosition());

}

Upvotes: 4

Related Questions