Android Lifecycle


#1

Hi all,

I am looking at the kindle version of Android Programming, and I have some questions regarding the statements:

[quote]However, you can always rely on onPause() and onSaveInstanceState to be called.
[/quote]Unfortunately with the Kindle there are no page numbers that I can usefully quote.

From some experimenting on my device (Nexus 7, 2ndgen) and a glance at the Android forums (http://developer.android.com/reference/android/app/Activity.html), it seems that you cannot rely on onSaveInstanceState to be called always. It says there that:

One example of this is on my device, if I open my activity and then finish() it via the back button, it goes through on{Pause, Stop, Destroy} without passing through onSaveInstanceState.

I’m not sure if I’m misunderstanding the book or it is a typo in the book, so I would appreciate some clarification. Thanks!


#2

The onSaveInstanceState method is not part of the lifecycle. It will be called when an activity is paused or stopped.
For example if you rotate your device, you can use the onSaveInstanceState method to save your data.
However if you want your data to persist for a longer time (i.e. if your activity is destroyed and the user comes back) then you should not rely on onSaveInstanceState and use some other mechanism like saving in files or using SQLite etc.
Please correct me if I am wrong.