When to use getSharedPreferences vs savedInstanceState?

I am trying to figure out when to use the saved state of an instance or loading information from a general settings file. I have two variables that I want to save, time and expense. I want to make sure that if the user returns to the game screen, their score and time will be saved and restored, regardless of whether they are included in onPause or onStop.

I have three keys:

public static final String ARG_SCORE = "score"; public static final String ARG_TIME = "time"; public static final String SHARED_PREFS = "shared_preferences"; 

If the game is paused and a dialog is displayed when the user returns, I must

 public void onRestoreInstanceState(Bundle savedInstanceState){ int score = savedInstanceState.getInt(ARG_SCORE); } 

or I need to do something like:

 protected void onResume(){ SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); int score = sharedPref.getInt(getString(R.string.saved_high_score)); } 

In general, I need help understanding the life cycle and storing important information such as time and game rating. I just need to avoid rebooting the user in cases where they could not finish the game.

Finally, I assume that sharedPrefs saves the xml file. It's right? Does anyone have an xml sample for how my sharedPrefs should appear? Are the keys that are saved in savedInstanceState packages stored in xml files? If so, any examples? If not, where is the information stored?

THANKS!


edits:

ok cool beans. Thank you Another issue when defining a key for a key-value pair stored in sharedPreferences, such as

 public static final String ARG_SCORE = "score"; 

why is the score line stored? When will it ever be used? I always added a value to the key_value pair using something like

 args.putInt(ARG_TIMER, timerINT); 

and retrieved using

 scoreINT=savedInstanceState.getInt(ARG_SCORE); 

Why is a name required for the ARG_SCORE key? When do I need a name? Should it stay in type String?

+6
source share
5 answers

use saveInstanceState when you often move between activities and use SharedPreferences , when you want to store information for a long time and yes sharedpreferences stored in the xml file. you can view using DDMS in eclipse.

Remeber , in saveInstanceState, when you close the application, it means that it is deleted from memory, information will also be lost. And in SharedPreferences, the information will remain there if you close your application.

+8
source

This will depend on how you want to manage the data. Both options (and more) are possible:

  • If you want to fill out once and save the data, even if the application is killed, use SharedPreferences.
  • If it is volatile data that needs to be re-entered in different ways, some at a different time (i.e. after a few days), then use onSavedInstanceState.
  • If you want to save multiple datasets on one device, use

    SQLiteDatabase

+2
source

Usually you want to use SharedPreferences when you want to store some information between different application sessions. Imagine that you want to store the information you want to receive, also after the user closes the application.

SavedInstanceState is used to store some information when the user uses the application, and allows you to track the temporary state of your activity or fragments.

Hope this helps.

+1
source

when you click the home button and then your activity stays in the background. since there are some memory limitations in android, there is always a chance that some other application can take your memory. therefore, to resume the application from the same point where we left, we use saveInstanceState. we use sharedprefrence when we need to save small information (usually a primitive type), such as a high score for a game in any game.

+1
source

The Android documentation says that SharedPreferences is XML , but there is no need to use SharedPreferences , if you do not want the data to be saved forever, you can save the game state using the Activity lifecycle methods without problems, but, for example, if the user turns off phone or press the "Back" button to complete the Activity , then savedInstanceState will not work, and you will lose your data.

This is your call if you want the game to be saved even if the user disconnected his phone (I think it would be quite radical, but if this is your requirement to continue), use SharedPreferences or DB if this is complex data. If you want the game to be saved only if the user navigates to your application, you can use savedInstanceState .

+1
source

Source: https://habr.com/ru/post/972447/


All Articles