Android app Development sharedpreferences How to use stored data
Sharedpreferences is the easiest data storage technology to understand in Android, and in fact sharedpreferences is dealing with a key-value (key-value pair) Sharedpreferences used to store some lightweight data.
1, the use of sharedpreferences to save data methods are as follows:
Instantiating a Sharedpreferences object (first step)
Sharedpreferences mysharedpreferences= getsharedpreferences ("Test",
Activity.mode_private);
Instantiating a Sharedpreferences.editor object (step Two)
Sharedpreferences.editor Editor = Mysharedpreferences.edit ();
Save the data in a putstring way
Editor.putstring ("name", "Karl");
Editor.putstring ("Habit", "sleep");
Submit Current Data
Editor.commit ();
To write data successfully using the TOAST information hint box
Toast.maketext (This, "data successfully written to sharedpreferences! ", Toast.length_long). Show ();
Execute the above code, Sharedpreferences will save this data in the Test.xml file, you can export it under the data/data/corresponding package name/test.xml of the file explorer and view it.
2, the use of Sharedpreferences read data method as follows:
Similarly, an Sharedpreferences object is instantiated before reading sharedpreferences data
sharedpreferencessharedpreferences= getsharedpreferences ("Test",
Activity.mode_private);
Use the GetString method to get value, note that the 2nd parameter is the default value of value
String name =sharedpreferences.getstring ("name", "");
String habit =sharedpreferences.getstring ("Habit", "" ");
Display information using the Toast Information prompt box
Toast.maketext (This, "read the data as follows:" + "\ n" + "name:" + name + "\ n" + "habit:" + habit,
Toast.length_long). Show ();
How to use the "Mark" Android app to develop sharedpreferences storage data