Why is it sometimes saved to save Hashtable objects not related to Parcelable in onSaveInstanceState ()?

After going through the introductory book on Android programming, I wanted to change the sample application to strengthen my understanding of some topics that were not really covered. When making the changes, I made a mistake, but I'm curious why the error worked in some cases, but not in others.

Activity in the application stores a number of questions in the Hashtable<Integer, Question> , where Question is a small class containing an int and two lines. As originally written, activity loads questions from the server on each onCreate() , so I wanted to implement onSaveInstanceState() to prevent some redundant downloads. onSaveInstanceState() saves the Hashtable in the Bundle using putSerializable() .

 @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); // mQuestions is a member variable of // type Hashtable<Integer, Question> if (mQuestions != null && mQuestions.size() > 0) { outState.putSerializable(SAVED_QUESTIONS, mQuestions); } } 

It worked great to change the screen orientation even before I knew what Parcelable was and how to implement it. I only knew that there was a problem when I pressed the home key of the emulator and the application silently, invisibly crashed without LogCat output. The stack trace led me to search for Parcelable and made Question implement it.

My question is not what I did wrong. The question is: When the Question class did not implement Parcelable, why did the application crash only when you click Home, and not when changing the screen orientation?

+6
android android-activity bundle parcelable activity-lifecycle
source share
3 answers

As far as I understand, Android does not serialize the state of the instance when the action is recreated after a configuration change. This is why your code works. Stored objects simply do not have to be explainable, since they exist only in memory.

It looks like an optimization. Android knows that in this case the process will not be completed, and there is no need to save the state of the instance to a file. (Theoretically, the process could be interrupted during a configuration change, and I really don't know how Android solves this problem).

But when the user presses the "Home" key, your application becomes the background. And its process can be terminated in case of low memory. Android needs to save the activity state to a file so that you can restore the application and its actions in the future. In this case, the state of the instance is indeed serialized and stored in persistent storage. And so your code is not working.

Termination of the process can occur at any time, so you cannot rely on some implementation details. Just make the state of the instance simple or serializable and you will no longer run into this problem.

+2
source share

Quote: Steve Mosley

Please note that it is NOT safe to use onSaveInstanceState and onRestoreInstanceState in accordance with the activity status documentation in http://developer.android.com/reference/android/app/Activity.html .

The document states (in the Activity Life Cycle section):

Note that it is important to keep persistent data in onPause() instead of onSaveInstanceState(Bundle) because later it is not part of the lifecycle callbacks, so it will not be in every situation, as described in its documentation.

In other words, add save / restore code to onPause() and onResume() !

+1
source share

The application did not work. It simply disconnected when the user clicked the Home button. That's why there was no way out in LogCat.

Set a breakpoint in Activity.onDestroy () to confirm this. If I'm right, onDestroy () will be called, but onSaveInstanceState () will not be called, because onSaveInstanceState () is called only when the application is placed in the background, and not when it is disconnected.

If you need to save the state of the application after shutting down, put the code in onDestroy () and save it to something more stable than the Bundle.

Barry

0
source share

All Articles