Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 688
Cygnus Avatar asked Feb 18 '13 12:02

Cygnus


1 Answers

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.

like image 181
class stacker Avatar answered Sep 30 '22 01:09

class stacker