[Apidemos for Android samples video series] app-activity-persistentstate

Source: Internet
Author: User
App-Activity-PersistentState1.Demo description and demo

This demo uses sharedpreference to save the UI status, mainly to demonstrate the simple use of sharedpreference, And the UI status is generally saved in onsaveinstancestate.

Through this demo, we can learn:

 

  • Simple use of sharedpreference
  • Introduction to preferencemanage
  • Use of scrollview
  • Introduction to horizontalscrollview

 

As follows:

2. Video Explanation and HD video download

Http://www.eyeandroid.com/forum.php? MoD = viewthread & tid = 10285 & extra = Page % 3d1

3. Demo Analysis

Android provides a variety of data storage methods, the simplest of which is to use shared preferences. sharedpreferences can store key/value pairs. sharedpreferences 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 sharedpreferences by using the getsharedpreferences (string name, intmode) 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)

/**

* Any time we are paused we need to save away the current state, so it will be restored correctly when we are resumed.

*/

@ Override

Protectedvoid 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 was changed at any time while paused.

*/

@ Override

Protectedvoid 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 );

}

}

}

4. We learned this demo.

1. sharedpreferences for Android Data Storage

Http://www.eyeandroid.com/forum.php? MoD = viewthread & tid = 9608.

2. Use of Android sharedpreference Mode

Http://www.eyeandroid.com/forum.php? MoD = viewthread & tid = 7533.

3. scrollview of Android Control

Http://www.eyeandroid.com/forum.php? MoD = viewthread & tid = 7615.

4. Android. View. View

Http://www.eyeandroid.com/forum.php? MoD = viewthread & tid = 10160.

5. scrollview refrence

Http://www.eyeandroid.com/forum.php? MoD = viewthread & tid = 10161.

 

5. Official Q & A website

Android Development Forum: http://www.eyeandroid.com

Android ask me: http://www.eyeandroid.com/forum.php? MoD = Forumdisplay & FID = 195

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.