4

UPDATE

The only workaround I was able to find was to create a static variable and store the state in there. This is not a good solution, but it works.

I tried saving the state in the Activity but the Activity's onRestoreInstanceState was never called.


I am not using setRetainInstance().

When on a fragment and I rotate the device or multi-task to another app and then come back, the savedInstanceState is not null in onCreateView. BUT when I stay in the app and click an item in the fragment's ListView and use an Intent to show a different activity and then hit the back button to return to the original activity (which shows my fragment), then it is null! Why?

NOTE: The answer in this How can I maintain fragment state when added to the back stack? did not work. My instance variables are in their default states when I return from the other activity.

//It is a android.support.v4.app.Fragment;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    //Only shows null when returning from another Activity IN this app
    Log.v(getClass().getSimpleName(), "NULL?" + savedInstanceState);

    return createMyView();
}

@Override
public void onSaveInstanceState(Bundle outState) {
    //Save some value (It is NOT null here)
    outState.putInt("TestKey", "saved_key" );

    //Save the state
    super.onSaveInstanceState(outState);

    //This is never null, so should be ok
    Log.v(getClass().getSimpleName(), "Outstate : " + outState);
}

@Override
public void onViewStateRestored(Bundle savedInstanceState) {
    //SavedInstanceState is NULL here too (when returning from within same app)!
    super.onViewStateRestored(savedInstanceState);
}

Why is it null when returning from another Activity but not when returning from other apps?

Community
  • 1
  • 1
Don Rhummy
  • 24,730
  • 42
  • 175
  • 330

0 Answers0