onSaveInstanceState is not saving my values ( onCreate input Bundle is always null )

前端 未结 6 1390
轻奢々
轻奢々 2020-12-10 12:19

Saving bundle (activity A):

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putString(\"test\", \"value\");
    super.onSaveInst         


        
相关标签:
6条回答
  • 2020-12-10 12:39

    In case you want to retain the state of your fragment over a rotation of the device and you wonder that onSaveInstanceState is never called:

    1. Check whether you call super.onSaveInstanceState(outState); in all your onSaveInstanceState functions.
    2. Check that you don't have android:configChanges in your AndroidManifest.xml.
    0 讨论(0)
  • 2020-12-10 12:49

    You have to do super.onSaveInstanceState(outState) before adding content to it.

    0 讨论(0)
  • 2020-12-10 12:56

    If navigating to another Activity and coming back, best way is to save it to SharedPreference in the onPause() method, which is bound to execute when a new activity is loaded. On the other Activity, the value can be accessed in onCreate() by accessing the shared preference.

    0 讨论(0)
  • 2020-12-10 12:58

    I got this solve by having:

    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    

    when re-launching the old intent that has the saved state from background. Without this flag, I think that when startActivity is called, it creates a new instance of the Activity instead of getting the old one from the stack.

    0 讨论(0)
  • 2020-12-10 12:59

    Unless your app is running on Lollipop (API21) version of Android or newer, your

    public void onSaveInstanceState (Bundle outState, PersistableBundle outPersistentState);
    

    will NOT be called as it simply does not exist on earlier versions of the platform than 21. To support pre API 21 devices you must, instead of the above, override the following method:

    public void onSaveInstanceState (Bundle outState);
    

    This will work on API 21+ as well, so you do not need to override both methods, of course (unless you know you need to deal with PersistableBundle the new one offers).

    (Copied because it worked for me) Source: onSaveInstanceState is not getting called after screen rotation

    0 讨论(0)
  • 2020-12-10 13:00

    There is nothing wrong with your code. Try rotating the device when on activity A. You will see the following in the Log, which means your onSaveInstanceState() is working fine:

    saved instance is nullfalse
    

    Here are an excerpt from the Android Developer Site, which you may find interesting:

    The callback method in which you can save information about the current state of your activity is onSaveInstanceState(). The system calls this method before making the activity vulnerable to being destroyed and passes it a Bundle object. The Bundle is where you can store state information about the activity as name-value pairs, using methods such as putString(). Then, if the system kills your activity's process and the user navigates back to your activity, the system passes the Bundle to onCreate() so you can restore the activity state you saved during onSaveInstanceState(). If there is no state information to restore, then the Bundle passed to onCreate() is null.

    Note: There's no guarantee that onSaveInstanceState() will be called before your activity is destroyed, because there are cases in which it won't be necessary to save the state (such as when the user leaves your activity using the BACK key, because the user is explicitly closing the activity). If the method is called, it is always called before onStop() and possibly before onPause().

    0 讨论(0)
提交回复
热议问题