tags:

views:

51

answers:

1

To check does preferences exist I tried this way, but it shows null every time(maybe because I saved preferences in different view):

String def = null; 
String test = getPreferences(MODE_PRIVATE).getString(PREF_GAME,def);
if(test == null) Log.v("main", "no saved data");

To delete preferences I tried editor.clear(), but it does't delete (however commit() every time return true):

   SharedPreferences preferences = getSharedPreferences(PREF_GAME,MODE_PRIVATE);
   SharedPreferences.Editor editor = preferences.edit();
   editor.clear();
   boolean tt = editor.commit();Log.v("DELETE PREF", String.valueOf(tt));

UPDATE: I found that if I check preferences exist in the same view, where i saved it, this checking works fine, but how can i do this in different view?

Update: I guessed with myself, thanks everyone!

+1  A: 

Use getSharedPreferences() to get your preference.

getSharedPreferences() - Use this if you need multiple preferences files identified by name, which you specify with the first parameter.

getPreferences() - Use this if you need only one preferences file for your Activity. Because this will be the only preferences file for your Activity, you don't supply a name.

Georgy Gobozov