Android ApiDemos example (9): App-& gt; Activity-& gt; Persist

Source: Internet
Author: User

Android provides a variety of data storage methods, the simplest of which is to use Shared Preferences. shared Preferences can store Key/value pairs. Shared Preferences supports accessing boolean, float, long, integer, string. The most common use of Shared Preferences is to store some application Preferences. In addition, onSaveInstanceState () is used to save the UI state.

App-> Activity-> Persistent State uses Shared Preferences to maintain some UI States (the value of TextView ).

Create or modify Shared Preferences by using the getSharedPreferences (String name, int mode) method. Shared Preferences is used to share some data between different activities of a single Application. A single Preferences cannot be used to share data between different applications.

SharedPreferences. Editor is used to add data to Shared Preferences: editor. putXXX (key, value)

[Java]
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 ();
}
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)

[Java]
Protected void onResume (){
Super. onResume ();

SharedPreferences prefs = getPreferences (0 );
String restoredText = prefs. getString ("text", null );
If (restoredText! = Null ){
MSaved. setText (restoredText, TextView. BufferType. EDITABLE );

Int selectionStart = prefs. getInt ("selection-start",-1 );
Int selectionEnd = prefs. getInt ("selection-end",-1 );
If (selectionStart! =-1 & selectionEnd! =-1 ){
MSaved. setSelection (selectionStart, selectionEnd );
}
}
}
Protected void onResume (){
Super. onResume ();
 
SharedPreferences prefs = getPreferences (0 );
String restoredText = prefs. getString ("text", null );
If (restoredText! = Null ){
MSaved. setText (restoredText, TextView. BufferType. EDITABLE );
 
Int selectionStart = prefs. getInt ("selection-start",-1 );
Int selectionEnd = prefs. getInt ("selection-end",-1 );
If (selectionStart! =-1 & selectionEnd! =-1 ){
MSaved. setSelection (selectionStart, selectionEnd );
}
}
}

 

Persistent State demonstrates how to use Shared Preferences to retain EditText content during Activity restoration. The more general method is to use onSaveInstanceState.

 


Author: mapdigit

Related Article

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.