38

Can I use savedInstanceState() to save the state when removing a fragment, then restore the state when I pop the fragment off the back stack? When I restore the fragment from the back stack, savedInstanceState bundle is always null.

Right now, the app flow is: fragment created -> fragment removed (added to back stack) -> fragment restored from back stack (savedInstanceState bundle is null).

Here is the relevant code:

public void onActivityCreated(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle bundle = getArguments();
    Long playlistId = bundle.getLong(Constants.PLAYLIST_ID);
    int playlistItemId = bundle.getInt(Constants.PLAYLISTITEM_ID);

    if (savedInstanceState == null) {
       selectedVideoNumber = playlistItemId;
    } else {
       selectedVideoNumber = savedInstanceState.getInt("SELECTED_VIDEO");
    }
}

public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(Constants.SELECTED_VIDEO, selectedVideoNumber);
    }

I think the problem is that onSavedInstanceState() is never called when being removed and being added to back stack. If I cant use onsavedInstanceState(), is there another way to fix this?

heero
  • 1,851
  • 5
  • 22
  • 31

4 Answers4

7

onSaveInstanceState is (unfortunately) not called in normal back-stack re-creation of a fragment. Check out http://developer.android.com/guide/components/fragments.html#Creating and the answer on How can I maintain fragment state when added to the back stack?

Community
  • 1
  • 1
Eric
  • 5,038
  • 6
  • 27
  • 33
5

I like to store the View I return in onCreateView as a global variable and then when I return I simply check this:

if(mBaseView != null) {
        // Remove the view from the parent
        ((ViewGroup)mBaseView.getParent()).removeView(mBaseView);
        // Return it
        return mBaseView;
    }
AllDayAmazing
  • 2,262
  • 1
  • 22
  • 23
  • 6
    not sure this is a good idea. Does this defeat the purpose of destroying the view to free memory if you save a reference to it? – craigrs84 May 13 '14 at 15:56
  • I'm actually not sure, as I haven't run any direct tests. However, my thinking goes like this. When you initialize all the elements in your layout, it's standard to set most of those elements as a global variables, so if in the above example I couldn't recycle the base View because of the reference, Android wouldn't be able to recycle any of these Views either, thus leading to the same issue with memory. – AllDayAmazing May 13 '14 at 19:12
  • 2
    This won't work if you need different views to be inflated for different orientations. – bcorso Jul 17 '14 at 21:37
  • 1
    This solution saved my life. – brainmurphy1 Jul 31 '14 at 15:44
3

The problem is that the fragment needs to have an Id or Tag associated with it in order for the FragmentManager to keep track of it.

There are at least 3 ways to do this:

  1. In xml layout declare an Id for your fragment:

    android:id=@+id/<Id>
    
  2. If your fragments container View has an Id, use FragmentTransaction:

    FragmentTransaction  add (int containerViewId, Fragment fragment)
    
  3. If your fragment is not associated with any View (e.g. headless fragment), give it a Tag:

    FragmentTransaction  add (Fragment fragment, String tag)
    

Also, see this SO answer.

Community
  • 1
  • 1
bcorso
  • 40,568
  • 8
  • 56
  • 73
0

FWIW, I hit this as well, but in my case onSaveInstanceState was called properly and I pushed in my state data when a new activity fragment was brought up on the smartphone. Same as you, the onActivityCreated was called w/ savedInstanceState always null. IMHO, I think it's a bug.

I worked around it by creating a static MyApplication state and putting the data there for the equivalent of "global variables"...

kenyee
  • 1,989
  • 21
  • 30