Shared preferences

This is used to keep small data from session to session, for example the settings of an app or the scores of a game.

You define a "key" for the data.

final String TEXTSIZESTRING="unique key for your value";

and then, in onCreate, you retrieve it using shared preferences.

SharedPreferences sp=getPreferences(MODE_PRIVATE); Val.TextSize=sp.getFloat(Val.TEXTSIZESTRING,20);
In this case, the default value is 20.
Before exiting the app, you must write the new values for the data you want to keep.

SharedPreferences sp=getPreferences(MODE_PRIVATE); SharedPreferences.Editor e=sp.edit(); e.putFloat(Val.TEXTSIZESTRING,Val.TextSize); e.commit();

No comments:

Post a Comment