When to use getSharedPreferences vs savedInstanceState?
Asked Answered
V

5

14

I'm trying to figure out when to use a saved instance state versus loading information from a shared preferences file. I have two variables that I wish to save, time and score. I want to make sure that if the user returns to the game screen that their score and time is saved and restored regardless if it's from onPause state 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 shown, when the user returns should I do

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

or should I do something like:

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

Overall, I need help understanding the lifecycle and when to store vital information such as time and score of the game. I simply need to avoid the user having to restart in cases they weren't able to finish the game.

Lastly, I'm assumed that the sharedPrefs saves to an xml file. Is this correct? Does anyone have a sample xml for how my sharedPrefs should appear? Do keys which are saved to bundles of savedInstanceState get stored in xml files as well? If so, any examples? If not, where is the information stored?

THANKS!


edits:

ok cool beans. Thanks! One more question, when defining a key for a key-value pair stored into sharedPreferences such as

public static final String ARG_SCORE = "score";

why is the "score" string stored? When would this ever be used? I've always placed a value into the key_value pair using something like

args.putInt(ARG_TIMER, timerINT);

and retrieved using

scoreINT=savedInstanceState.getInt(ARG_SCORE);

Why is a name needed for the key ARG_SCORE? When would I need the name? Does it have to stay type String?

Vesuvian answered 18/7, 2014 at 9:51 Comment(2)
SharedPreferences are saved in some file as simple key/value pairs . They are a persistent storage, so what you save in there stays there until you overwrite it. OnSaveInstanceState as you call it is something completely different. It is used internally by the OS to store and restore Activity instance from the back stack.Thumbtack
google-developer-training.gitbooks.io/…Wegner
S
15

use saveInstanceState when you are frequently moving back and forth between activities and use SharedPreferences when you want to keep information for long time and yes sharedpreferences stored in an xml file. you can view using DDMS in eclipse.

Remeber, in saveInstanceState, when you close app mean it get removes from memory, information will also lost. And in SharedPreferences, information will remain there if you close your app.

Sutherland answered 18/7, 2014 at 9:53 Comment(3)
ok cool beans. Thanks! One more question, when defining a key for a key-value pair stored into sharedPreferences such as (public static final String ARG_SCORE = "score";)why is the "score" string stored? When would this ever be used? I've always placed a value into the key_value pair using something like (args.putInt(ARG_TIMER, timerINT);) and retrieved using (scoreINT=savedInstanceState.getInt(ARG_SCORE);). Why is a name needed for the key ARG_SCORE? Does it have to stay type String?Vesuvian
"score" is stored in a variable SCORE so when you put ARG_SCORE in key , its value will be treated as key not itself.Sutherland
I'm not quite sure I understand. -so the string "score" is stored in ARG_SCORE -a value, let's say 5 is stored into ARG_SCORE -ARG_SCORE is stored in a file via args.putInt(ARG_TIMER, 5); I don't understand what you mean by: -"'score' is stored in a variable SCORE"; (I assumed you meant ARG_SCORE instead of SCORE?) -"so when you put ARG_SCORE in key"; (Isn't ARG_SCORE the key?) -"its value will be treated as key not itself"; (its meaning ARG_SCORE? value being 5? So are you saying that 5 is being treated as the key?)Vesuvian
P
2

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

  • If you want to fill once and keep the data even if the app gets killed, use SharedPreferences.
  • If it's volatile data that will have to be reentered differently some other time (i.e., days later), then use onSavedInstanceState.
  • If you want to keep multiple datasets on the same device, then use a

    SQLiteDatabase

Pyrope answered 18/7, 2014 at 9:55 Comment(0)
S
2

You usually want to use SharedPreferences when you want to persist some information between different app session. Imagine you want to store information that you want to retrieve also after the user closes the app.

SavedInstanceState is used to keep some information while user is using the app and allow you to track temporary state of your activity or fragments.

Hope it helps.

Sternutatory answered 18/7, 2014 at 9:57 Comment(0)
P
1

when you press home button then still your activity remains in background. since there is some memory constraints in android , there is always chance some other application can take your memory. so to resume application from same point where we have left we use saveInstanceState. we use sharedprefrence when we have to save small info(normally primitive type) like game high score in any game.

Pluralize answered 18/7, 2014 at 9:59 Comment(0)
C
1

In the Android documentation says how to relate SharedPreferences to XML but there's no need to use SharedPreferences if you don't want the data to be stored forever, you can store the game's state using the Activitys lifecycle methods with no problem, but for example, if the user turns off it's phone or presses the back button to finish your Activity, then the savedInstanceState won't work and you will lose your data.

It's your call, if you want the game to be saved even if the user turns off his phone (I think this would be kinda radical, but if it's your requirement go ahead) then use SharedPreferences or a DB if it's complex data. If you want the game to be saved only when the user navigates in and out to your app, then it's safe to use the savedInstanceState.

Chasechaser answered 18/7, 2014 at 10:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.