Fragment's Onsaveinstancestate() Is Never Called

onSaveInstanceState not called on Fragment

"In a Fragment, all of their lifecycle callbacks are directly tied to their parent Activity. So onSaveInstanceState gets called on the Fragment when its parent Activity has onSaveInstanceState called."

Look at this post:

FragmentActivity onSaveInstanceState not getting called

Fragment's onSaveInstanceState() is never called

I finally figured out the problem, at least in my case. I had an overridden onSaveInstanceState in my FragmentActivity that did not call super.onSaveInstanceState(Bundle outState). Once I added that in, the Fragment.onSaveInstanceState(Bundle outState) functioned normally.

Why isn't my fragments onSaveInstanceState() being called?

You can enable myFragment.setRetainInstance(true) to retain the state, but it does this 'automatically', i.e. it retains the values assigned to your class members and does NOT use the onSaveInstanceState (and hence savedInstanceState is always null).

Make sure the FragmentActivity that hosts this fragment does not override onSaveInstanceState or when it does it should call super.onSaveInstanceState(Bundle).

onSaveInstanceState is not being called in Fragment

onSaveInstanceState() is only called when the Android system may need to recreate that particular instance of the Fragment. There are many instances of navigating away from the Fragment or even destroying the Fragment where it will not be called. Please consult the documentation for more information, please read the Activity.onSaveInstanceState() documentation as well since that applys here also: http://developer.android.com/reference/android/app/Fragment.html#onSaveInstanceState(android.os.Bundle)
http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle)

EDIT: In your case, you must not recreate the fragment every time the user navigates back to the fragment. I recommend considering ViewPager and FragmentPagerAdapter to automatically manage the fragments. Also look at this post: ViewPager and fragments — what's the right way to store fragment's state?

FragmentActivity onSaveInstanceState not getting called

The issue here is that you are misunderstanding how onSaveInstanceState works. It is designed to save the state of the Activity/Fragment in the case that the OS needs to destroy it for memory reasons or configuration changes. This state is then passed back in onCreate when the Activity/Fragment is returned to / restarted.

In a Fragment, all of their lifecycle callbacks are directly tied to their parent Activity. So onSaveInstanceState gets called on the Fragment when its parent Activity has onSaveInstanceState called.

When pausing the activity (using the back button), the onSaveInstanceState is never called, and consequently, savedInstanceState is always null within the onCreate method upon resuming the app.

When pressing back, the user is destroying the Activity, and therefore its children Fragments, so there is no reason to call onSaveInstanceState, since the instance is being destroyed. When you reopen the Activity, it's a brand new instance, with no saved state, so the Bundle passed in onCreate is null. This is behaving exactly as designed. However, try rotating the device or hitting the home button, then you will see the Activity and its children Fragments have onSaveInstanceState called, and passed back in onCreate when returned to.

The hack you added, directly calling onSaveInstanceState(new Bundle()); inside of onPause, is a very bad practice, as you should never call the lifecycle callbacks directly. Doing so can put your app into illegal states.

If what you really want is for your data to persist beyond an instance of your app, I suggest you look into using SharedPreferences or databases for more advanced data. You can then save your persistent data in onPause() or whenever it changes.

savedInstanceState is null and onSaveInstanceState is not getting called

In Android, onSaveInstanceState() will be called by the system to save the current state of the activity to make sure when users resume the app they will see the activity when they left before (by calling onRestoreInstanceState() to restore the state of activity). It will be called in the following conditions:

  • Users move from an activity to another activity

  • Users press the Home key to bring the app to the background

  • When configuration changed, like users rotate the phone or change language, etc.

In your case, because you press the Back key, so the system knows that you want to leave the activity, so no need to save the state of that activity, that why you never see onSaveInstanceState() and onRestoreInstanceState() get called.



Related Topics



Leave a reply



Submit