"Sail Plan 008" 2015 sail plan Android Apidemo Devil Step app->activity->persistent State Save status Sharedpreferences OnPause Onresume

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.