In the previous article, I temporarily saved the state of the Activity in the onSaveInstanceState method. Although this is also useful, in more cases, we prefer to persistently store some simple data, such as saving the settings of some users or storing the highest score of the current game. In this blog, let's use shared preferences to save some simple data.
Following the example in the previous section, in mainactivity In Java, overload an onPause method and add the following code
@Override protected void onPause() { super.onPause(); SharedPreferences settings = getPreferences(MODE_PRIVATE); SharedPreferences.Editor editor = settings.edit(); editor.putInt(KEY_COUNTER, mCounter); editor.commit(); Log.i("SharedPreferences","SharedPreferences has been writen ,mCounter = " + mCounter); }
A SharedPreferences object is obtained by getPreferences method, and an editor object of SharedPreferences is obtained by calling edit method of SharedPreferences object, then putXXX can be called to store a specific type of key value pair. Finally, editor. is called. Commit() to save the settings.
Then, read the value stored in SharedPreferences in the onCreate method.
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); SharedPreferences settings = getPreferences(MODE_PRIVATE); int defaultCounter = 0; mCounter = settings.getInt(KEY_COUNTER, defaultCounter); ((TextView)findViewById(R.id.textViewCounter)).setText("Counter: " + Integer.toString(mCounter)); }
Or get the SharedPreferences object through the getPreferences method, which can directly call the getXXX method to get the value.
At this time, I made a small test. In another activity, after obtaining the SharedPreferences object through the getPreferences method, the value we just saved cannot be obtained. Why? In fact, there is another method to obtain the SharedPreferences object, that is, getSharedPreferences(name,mode) method. The first name is a file name, and each name corresponds to a file, which means that we can store different types of configuration items in different files, and the file format is XML. Of course, These basic functions are already provided by the Android system. We just need to simply call the api. The getPreferences method passes the full name of the current activity class as the name to the getSharedPreferences method.
Next, we modify the code a little so that we can save data in one activity and read data in another activity.
The following code is used to save the status to the SharedPreferences configuration file named mytest when the app is in the stopped state.
public final static String PREF_NAME="mytest"; @Override protected void onPause() { super.onPause(); SharedPreferences settings = getSharedPreferences(PREF_NAME,MODE_PRIVATE); SharedPreferences.Editor editor = settings.edit(); editor.putInt(KEY_COUNTER, mCounter); editor.commit(); Log.i("SharedPreferences","SharedPreferences has been writen ,mCounter = " + mCounter); }
Next, read the data just saved in the onCreate method of another activity
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); SharedPreferences settings = getSharedPreferences(MainActivity.PREF_NAME,MODE_PRIVATE); int defaultCounter = 0; int mCounter = settings.getInt(MainActivity.KEY_COUNTER,defaultCounter); Toast.makeText(this, mCounter+"", Toast.LENGTH_SHORT).show(); }
Easy to use. I wish your Android development path from getting started to giving up. Thank you!