Ok, this is how I have done the example of “Shared preferences”, this is in my helper preferences class:
public static final String GAME_PREFERENCES = "GamePrefs";
and this is in one of my activitie’s classes:
SharedPreferences settings =
getSharedPreferences(GAME_PREFERENCES, MODE_PRIVATE);
SharedPreferences.Editor prefEditor = settings.edit();
prefEditor.putString("lastLaunch", returnTimeAndDateFormatted());
prefEditor.commit();
SharedPreferences settings2 =
getSharedPreferences(GAME_PREFERENCES, MODE_PRIVATE);
if (settings2.contains("lastLaunch") == true) {
// System.err.println(settings2.getString("lastLaunch", "Default"));
Log.i("LASTLAUNCH", settings2.getString("lastLaunch", "Not LastLaunch value found!"));
}
As I understand it I can access the variable “lastLaunch” from any of my activity classes which is all fine and dandy.
My book says there is also something known as “activity-level preferences” but does not give an example 🙁
Can someone give me an example (code) of this please?
Thanks in advance!
An example would be to use the getPreferences() method of activity.
Here lastLaunch is private to this Activity.