android - Retrieving SharedPreferences Data Failure -


i trying use sharedpreferences, keep encountering following problem.

in class create file & add information following:

private sharedpreferences prefs         = null; private sharedpreferences.editor editor = null;  public void setstuff (string info, boolean save, context appct) {   if (prefs == null && editor == null) {     prefs  = appct.getsharedpreferences("preferences", context.mode_private);     editor = prefs.edit();   }   editor.putstring("custom", info);   editor.commit(); } 

which, knowledge, runs perfectly. however, when attempt access information stored in 'preferences' other class:

private static sharedpreferences prefs = null; private static string custom = null;  public static void getstuff(context appct) {   if (prefs == null) {     prefs = appct.getsharedpreferences("preferences", context.mode_private);     custom = prefs.getstring("custom", null);   } } 

the value of custom null, if never stored string. can tell me why is?

i have searched answer; using context getsharedpreferences, have following of instructions & tutorials have found no varying results.

note: classes not in same application. problem? can not access preferences of application?

thank you.

getstuff() sets custom if prefs null. so, method nothing after first time called. or, if prefs set before method ever called, method never anything.

the method should this, instead:

public static void getstuff(context appct) {   if (prefs == null) {     prefs = appct.getsharedpreferences("preferences", context.mode_private);   }   custom = prefs.getstring("custom", null); } 

also, if 2 classes in different applications, context.mode_private prevent them sharing sharedpreferences. can use context.mode_world_readable or context.mode_world_writeable instead, unsafe , deprecated in api level 17. see information @ above links see recommended instead.


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -