android - No default value in sharedpreferences -
a code snippet:
if (mgameprefs.contains(game_pref_name)) { thename = mgameprefs.getstring(game_pref_name, "jane doe"); }
but when retrieve preference without previous storage, there's no default value. retrieval code:
preferencelastgame gamesettings = new preferencelastgame(this); string gamename = gamesettings.getthename();
i have code default value
if(gamename == null) gamename = "jane doe";
every thing else in place , works fine. why failing?
game_pref_name define as:
public class memoinfoactivity extends fragmentactivity { public static final string game_preferences = "gameprefs"; public static final string game_pref_name = "thename"; // key string ........
preferencelastgame class put code regarding handling of preferences.
if (mgameprefs.contains(game_pref_name)) { thename = mgameprefs.getstring(game_pref_name, "jane doe"); }
you won't default value code. first thing check whether contains game_pref_name
. if doesn't, skip part would give default value.
just use:
thename = mgameprefs.getstring(game_pref_name, "jane doe");
Comments
Post a Comment