Data storage of sharePreference

Shared Preferences winform like to do some small configuration ini file, used to save some of the parameters set by the user. example

 

For example, it can be saved by the last changes made to the user or custom parameters set, when the program starts again still maintain the original settings

 

 

Shared Preferences processing data has three modes of operation modes MODE_PRIVATE is preferences. This is the default mode

 

Ceremony, representatives of the preferences only create a program to access the preferences. The other two modes are

 

MODE_WORLD_READABL和 MODE_WORLD_WRITEABLE。

MODE_WORLD_READABLE represent other programs on this SharedPreferences read-only access. MODE_WORLD_WRITEABLE 

 

Is the other program also has read and write permissions.

 

save data:

SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);

SharedPreferences.Editor editor = preferences.edit();

editor.putInt(”storedInt”, storedPreference); // value to store

editor.commit();

 

get data:

SharedPreferences preferences = getPreferences(MODE_PRIVATE);

int storedPreference = preferences.getInt(”storedInt”, 0);

 

Activity Preferences:

shared preferences can be used by other components of the program. But if you do not need to share components and other preferences,

 

But to a private in the activities preferences. GetPreferences activity class may be used () method

 

achieve. getPreference activity class method name as a parameter to call getSharedPreference ().

SharedPreferences preferences = getPreference(MODE_PRIVATE);

SharedPreferences.Editor editor = preferences.edit();

editor.putInt(”storedInt”, storedPreference); // value to store

editor.commit();

 

Save and restore the state of the instance:

onSaveInstanceState use the opportunity

1, when the user presses the HOME key.

  This is obvious, the system does not know how many other you press the HOME program to run, naturally did not know whether the activity A

 

It will be destroyed, so the system will call onSaveInstanceState, allowing users to have the opportunity to save some of the non-persistent data. The following situation

 

Analysis of the status of all follow this principle

  2, press the HOME key, choose to run other programs.

  3, when the power button is pressed (off screen).

  4, when starting a new activity from the activity of A.

  5, when the orientation changes such as switching from the portrait to the landscape.

  Before switching the screen, the system will destroy activity A, the system will automatically create activity A after screen switching, so

 

onSaveInstanceState will be executed

  All in all, calling onSaveInstanceState follow an important principle, that is, when you destroy the system "without your permission."

 

The activity, the onSaveInstanceState system will be called, which is the responsibility of the system, because it must provide an opportunity for

 

You save your data (of course you do not save it whatever you want)

As onRestoreInstanceState method should be noted that, onSaveInstanceState methods and 

 

onRestoreInstanceState method "is not necessarily" be paired is called, it is being called the premise onRestoreInstanceState

 

, Activity A "really" destroyed by the system, and if only there is the possibility to stay in the case, the method will not be transferred

 

With, for example, when activity A is being displayed, when the user presses the HOME key to return to the main screen, and then immediately returned to the user

 

activity A, which is generally not the case because of activity A memory system is destroyed, so that the activity A

 

onRestoreInstanceState method will not be executed

  In addition, onRestoreInstanceState the bundle parameters will be passed to onCreate method, you can also choose

 

The method for data reduction onCreate

@Override 

public void onSaveInstanceState(Bundle savedInstanceState) { 

  // Save UI state changes to the savedInstanceState. 

  // This bundle will be passed to onCreate if the process is 

  // killed and restarted. 

  savedInstanceState.putBoolean("MyBoolean", true); 

  savedInstanceState.putDouble("myDouble", 1.9); 

  savedInstanceState.putInt ( "Myint", 1); 

  savedInstanceState.putString("MyString", "Welcome back to Android"); 

  // etc. 

  super.onSaveInstanceState (savedInstanceState); 

 

@Override 

public void onRestoreInstanceState(Bundle savedInstanceState) { 

  super.onRestoreInstanceState (savedInstanceState); 

  // Restore UI state from the savedInstanceState. 

  // This bundle has also been passed to onCreate. 

  boolean myBoolean = savedInstanceState.getBoolean("MyBoolean"); 

  double myDouble = savedInstanceState.getDouble("myDouble"); 

  int myInt = savedInstanceState.getInt("MyInt"); 

  String myString = savedInstanceState.getString("MyString"); 

Guess you like

Origin blog.csdn.net/dxh040431104/article/details/6122406