Reputation: 3330
I have a confusion regarding what should go in onPause
,onStop
and onSaveInstanceState
. For example, the Android docs say that
For onPause
-
Commit unsaved changes, but only if users expect such changes to be permanently saved when they leave (such as a draft email).
For onStop
-
you should use onStop() to perform larger, more CPU intensive shut-down operations, such as writing information to a database.
For onSaveInstanceState
your activity might have more state information that you'd like to restore, such as member variables that track the user's progress in the activity.
Isn't it true that these three things basically point to storing information like of a form, or an email ? Then in which method should it be saved ?
Upvotes: 7
Views: 3060
Reputation: 10469
For anyone coming here in or after 2016, note that @Class Stacker's answer is quite out-dated at this point. Specifically, onStop()
is guaranteed to be called since Android 3.0 (Honeycomb), which accounts for 96.8% of Android devices as of January 2016.
See the lifecycle documentation here: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle (Ctrl+F for "not in the killable state")
UPDATE: as rightly pointed out in the comments, this of course means that your app must be limited exclusively to Android 3.0 and above.
Upvotes: 2
Reputation: 5347
Everything that you want to be persistent must be stored in onPause()
because some Android versions consider your app to be killable after onPause()
returned.
The somewhat unclear distinction is being made because onPause()
occurs relatively often, and also under many circumstances where you probably wouldn't save the state for an onResume()
of the same Activity. Hence, everyone wants you to think twice before you perform expensive operations in onPause()
.
Your question regarding storing form data, well, you could make that persistent right when an input field loses focus, if it's really totally intended that the user sees the same form data even after he stopped the app and started it again.
Upvotes: 6