简体   繁体   中英

onSaveInstanceState() and the bundle

It is about void onSaveInstanceState(Bundle saved) and a fundamental java concept.

A Bundle object must be created before calling this method, then it is passed to this method. Let's say it is created and passed like this:

Bundle savedInstanceState= new Bundle();
onSaveInstanceState(savedInstanceState);

This method saves data in that bundle. But it does not return that bundle (to which it has added the name-value pairs).

public void onSaveInstanceState(Bundle saved){
. . . //data added to the bundle named saved
} 

So, the bundle saved, which was declared as a parameter variable, has scope with in the method only. The data added to saved inside the method is not added to savedInstanceState. The method is also not returning anything.

So what is the significance of this method then when it is not changing any bundle outside itself and also not returning anything?

We also say that the bundle passed to onCreate carries the previously saved frozen state of the activity. It seems that this bundle comes from the processing in onSavedInstanceState(), but the method does not return anything and does not change the value of the bundle passed to it, outside itself. I am confused.

If somebody can help me understand this concept, I'll be grateful. Yes I have read the reference given in the Activity API.

Based on your question:

So what is the significance of this method then when it is not changing any bundle outside itself and also not returning anything?

I think you are completely missing the whole point of the method itself, even beyond the parameter that takes and gives you the chance to gather some information that might interest you after recreating, the point of this method is to let you know that a mechanism that could make an object lose it's state is about to start. You can notice for example that the method gets executed when your device is rotated and the "recreation" of the activity will impact the current state of the objects, giving you the chance to save information and make use of it in the "onRestoreInstanceState", also you might noticed that if you press back and close an activity it do NOT gets executed, because you no longer care about those objects state, so beyond whatever you get in the bundle(which is your responsibility to populate), the importance of this method is to let you know that something that will corrupt your state is about to start and you should take your precautions...

Hope this clarifies your question...

Regards!

So, lets say you override onSaveInstanceState method in your custom activiy, something like this :

public void onSaveInstanceState(Bundle dataToBeSaved) {
    super.onSaveInstanceState(dataToBeSaved);
    dataToBeSaved.putString("myKey", "myImportantStringValue"); 
    //populate bundle with more data
} 

Here is what happens (simplified) when your activity is being killed and has a chance to save some data :

  • Android system will create Bundle data = new Bundle(); as you correctly stated in your question.
  • Android will call your activity.onSaveInstanceState(data); passing reference to just created Bundle object.
  • your onSaveInstance method receives copy of that reference (named as dataToBeSaved inside your method). Note that this reference(and its copy) refer to the Bundle object managed by system. Your method will therefore populate this system-managed Bundle object.
  • System keeps modified Bundle object while your activity is being restarted
  • When your activity is back, system will call your activity.onCreate(data); passing [reference to] this previously stored Bundle
  • In your onCreate() you get a reference to a Bundle object with the same content as one you accessed in onSaveInstanceState()
    [ comment-based edit ] You will get reference to exactly same Bundle or its re-created duplicate depending on how system manages memory between activity/process restart and this is irrelevant for developer.

Hope that clarifies things for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM