Preserving State Information of an Activity (or Fragment)
Two kinds of data you want to preserve
-
shared document-like data (typically stored in a SQLite database using a content provider)
-
internal state such as user preferences.
shared document-like data
Edit in Place Concept --- Once a user makes edits you save them immediately
-
Example if the user chooses to write a new e-mail, a new database or file entry for that e-mail is created as soon as they start entering data, so that if they go to any other activity after that point this e-mail will now appear in the list of drafts.
- When creating a new document, the backing database entry or file for it is created immediately.
- onPause() implemented it should commit to the backing content provider OR file any changes the user has made.
- onResume() and onCreate(*) implemented - and this is where you go to database or file to retrieve any already saved data.
TIP: You will probably want to commit your data even more aggressively at key times during your activity's lifecycle: for example before starting a new activity, before finishing your own activity, when the user switches between input fields, etc.
OPTION 1: onPause()/onResume() and/or onCreate(*)= called when Activity killed or pushed into background
OPTION 2: onSaveInstanceState(*) and onResumeInstanceState(*)= called with activity ABOUT to be killed or put in background.. NOT FIRED when activity is unloaded from stack (i.e. user hit BACK button).
RECOMMENDATION: do the onPause() when want to handle all situations
Option 2: onSaveInstanceState()
- use Bundle object passed to it to save activity state
- later get bundle passed to onRestoreInstanceState(Bundle b) to get info you stored in it.
- NOTE: there is a method onRetainNonConfigurationInstance() paired with getLastNonConfigurationInstance()(this from onCreate) when you want to save data not in a Bundle.
@Override
public void onSaveInstanceState(Bundle outState) {//save what you need
outState.putString("ID", "123456");
super.onSaveInstanceState(outState);}
//restore the information
@Override
public void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
//retrieve the info you saved
String ID = savedIntancedSate.getString("ID"); //associated with the ID
//do what you want with it
}Option 1: onPause()
- onPause() is where you deal with the user leaving your activity. Most importantly, any changes made by the user should at this point be committed (usually to the ContentProvider holding the data).
- it should commit to the backing content provider or file any changes the user has made.
- You will probably want to commit your data even more aggressively at key times during your activity's lifecycle: for example before starting a new activity, before finishing your own activity, when the user switches between input fields, etc.
- onResume() is called when a paused Activity is brought back to run.
internal state such as user preferences.
-
when you need to remember the user's preferences
-
Examples: preferred initial display in a calendar (day view or week view) or the user's default home page in a web browser.
-
Associated with the Activity only: SharedPreferences from Activity.getPreferences(*)
- Activity.getPreferences(int), = use this to get instance of SharedPreferences associated with the Activity
Retrieves a SharedPreferences object for accessing preferences that are private to this activity. This simply calls the underlying getSharedPreferences(String, int) method by passing in this activity's class name as the preferences name.
Parameters
mode Operating mode. Use MODE_PRIVATE for the default operation, MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE to control permissions. - SharedPreferences.put*(Name, Value) -- putInt, putString -- to store the name,value pairs
- SharedPreferences.get*(Name, DefaultValue) --- getInt, getString -- to retrieve
Associated w/ multiple Application components-Activities, Recievers, Services, Providers: SharedPreferences from
Context.getSharedPreferences()
- Context.getSharedPreferences() = use to get intance of Shared Preferences associated with ALL Application components --ALL activities, etc.
- SharedPreferences.put*(Name, Value) -- putInt, putString -- to store the name,value pairs
- SharedPreferences.get*(Name, DefaultValue) --- getInt, getString -- to retrieve
method to retrieve a preferences object stored under a specific name. (Note that it is not possible to share settings data across application packages -- for that you will need a content provider.)
EXAMPLE Here is an excerpt from a calendar activity that stores the user's preferred view mode in its persistent settings:
public class CalendarActivity extends Activity {
...
static final int DAY_VIEW_MODE = 0; //2 modes to display calendar in -daily or weekly
static final int WEEK_VIEW_MODE = 1;
private SharedPreferences mPrefs;
private int mCurViewMode;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences mPrefs = getSharedPreferences();
mCurViewMode = mPrefs.getInt("view_mode", DAY_VIEW_MODE); //this mCurViewMode will be used
//later when some display is setup
}
protected void onPause() {
super.onPause();
SharedPreferences.Editor ed = mPrefs.edit(); //store the user's preference for view mode
ed.putInt("view_mode", mCurViewMode); //mCurViewMode was set somewhere else possibly
ed.commit(); //result of a GUI event handling method
}
}