Android provides a variety of ways to store data, the simplest of which is to use the shared Preferences. Shared Preferences can store key/value pairs, shared Preferences supports access to Boolean, float, long, Integer, string, most commonly used shared Preference S is used to store some application preferences. Another way to do this is to use Onsaveinstancestate (), which is especially useful for saving the UI state.
Another way to do this is to use Onsaveinstancestate (), which is especially useful for saving the UI state.
App->activity->persistent State uses shared preferences to maintain part of the UI status (the value of TextView).
Create or modify a shared Preferences, using the Getsharedpreferences (String name, int mode) method. Shared Preferences is used to share some data between different activity of a single application, but it cannot be used to share data between different application.
Sharedpreferences.editor used to add data to shared preferences: Editor.putxxx (key,value):
/** * any time we is paused we need to SA ve away the current state and so it * would be restored correctly if we are resumed. */ @Override protected void OnPause () { super .onpause (); Sharedpreferences.editor Editor = getpreferences (0). Edit (); Editor.putstring ( "text" , Msaved.gettext (). toString ()); Editor.putint ( "Selection-start" , Msaved.getselectionstart ()); Editor.putint ( "selection-end" , Msaved.getselectionend ()); Editor.commit (); }
Read shared Preference:pref.getXXX (key)
/*** Upon being resumed we can retrieve the current state. This allows us * to update the state if it is changed at any time while paused. */@Overrideprotected voidOnresume () {Super. Onresume (); Sharedpreferences prefs= Getpreferences (0); String Restoredtext= Prefs.getstring ("text",NULL); if(Restoredtext! =NULL) {Msaved.settext (Restoredtext, TextView.BufferType.EDITABLE); intSelectionStart = Prefs.getint ("Selection-start", 1); intSelectionend = Prefs.getint ("Selection-end", 1); if(SelectionStart! =-1 && selectionend! =-1) {msaved.setselection (SelectionStart, selectionend); } } }
Persistent state demonstrates how to use shared preferences to maintain edittext content when activity resumes. The more general approach is to use onsaveinstancestate.
"Sail Plan 008" 2015 sail plan Android Apidemo Devil Step app->activity->persistent State Save status Sharedpreferences OnPause Onresume