SharedPerference is different from file storage. It uses key values to store data. Each piece of data saved will be given a key value, so that the corresponding data is directly retrieved through the key value when reading the data. amdroid provides three methods to get instances:
getSharePreferences() method in class
It receives two parameters, the first is the file name; the second is the operation mode. Currently, only MODE_PRIVATE is optional. This is the default operation mode, which means that only the current application can operate on the file.
getPreference() method in class
It only receives one operation mode parameter, because using this method will automatically use the class name SharedPreference as the file name.
getDefaultSharedPreference() method in class
It is mainly achieved in three steps:
(1) Call the edit() method of the SharedPreferences object to get an object.
(2) Add data to the object, for example, if you add a Boolean data, use the putBoolean() method and deduce it in turn.
(3) Call the apply() method to submit the added data to complete the data operation. `
Use Cases
MainActivity:
package ; import ; import .; import ; import ; import ; import ; import ; public class MainActivity extends AppCompatActivity implements { private Button button; private Button restore_btn; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); button = (Button)findViewById(.save_data); (this); restore_btn = (Button)findViewById(.restore_data); restore_btn.setOnClickListener(this); } @Override public void onClick(View view) { switch (()){ case .save_data: saveData(); (,"Save successfully!",Toast.LENGTH_SHORT).show(); break; case .restore_data: restorData(); (,"Recovery Successfully",Toast.LENGTH_SHORT).show(); break; default: break; } } public void saveData(){ editor = getSharedPreferences("data",MODE_PRIVATE).edit(); ("name","Tom"); ("age",18); ("married",false); (); } public void restorData(){ SharedPreferences preferences = getSharedPreferences("data",MODE_PRIVATE); String name = ("name",""); int age = ("age",0); boolean married = ("marred",false); ("Main Activity: ","Get the name: "+name); ("Main Activity: ","Get age: "+age); ("Main Activity: ","Get the marriage: "+married); } }
Layout file
<?xml version="1.0" encoding="utf-8"?> < xmlns:andro xmlns:app="/apk/res-auto" xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=""> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android: android:text="Save Data"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android: android:text="Recover data"/> </LinearLayout> </>
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.