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?