A simple primer on data storage SharedPreferences
Implementation process: Simply put, it is to save an XML file (k, v) in the form
getSharedPreferences("demo1", MODE_PRIVATE);
The first parameter is: the saved file name
The second parameter is: the saved data mode (mostly PRIVATE)
Here we give an example:
SharedPreferences sp = getSharedPreferences("demo1", MODE_PRIVATE);
ed_ip.setText(sp.getString("ip", ""));
ed_port.setText(sp.getString("port", ""));
- The saved file name is demo1, and the mode is private
- ed_ip and ed_port are text input boxes, set their text here
- Use getString() to get the value
SharedPreferences.Editor editor = sp.edit();
editor.putString("ip", ed_ip.getText().toString());
editor.putString("port", ed_port.getText().toString());
editor.apply();
- Submit the value using putString()
- Finally, pay attention to calling apply() to submit data
Finally, let's look at where to save
Find the virtual machine file in the lower right corner
Data -> Data -> your package name -> shared_prefs -> file name
For example mine is: /data/data/com.example.myapplication/shared_prefs/demo1.xml
Open it and you can see the XML file named demo1 created by getSharedPreferences("demo1", MODE_PRIVATE), the content is as follows (K, V)