Cygnus Cygnus - 5 months ago 28
Android Question

android - data stored in onPause, onStop or onSaveInstanceState

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 ?

Answer

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.

Comments