I save the value in one activity like this:

sPref = getPreferences(MODE_PRIVATE); SharedPreferences.Editor edit = sPref.edit(); edit.putBoolean(ON_CAMERA, true); edit.commit(); 

And I get in another way:

 sPref = getPreferences(MODE_PRIVATE); Boolean bl = sPref.getBoolean(ON_CAMERA, false); 

The problem is that bl always false . After reading the literature, I noticed that SharedPreferences works with only one activity. Tell me how to save and get the value correctly?

PS I save the setting value, which is true or false (the default) in one activation, and in the other I should get this value, depending on which change the behavior of the button.

  • Are you sure that you have the same ON_CAMERA both there and there? - Vladyslav Matviienko
  • @metalurgus, yes - Amandi

1 answer 1

To obtain an instance of the SharedPreferences class, three methods are used to access the settings in the application code:

getPreferences() - inside the activity to access the activity-specific preference;
getSharedPreferences() - inside the activity to access application-level preferences;
getDefaultSharedPreferences() - from the PreferencesManager object to get the public setting provided by Android.

To access the settings from anywhere in the application, you need to use the second or third method:

 SharedPreferences sharedPreferences = getSharedPreferences(APP_PREFERENCES, Context.MODE_PRIVATE); 

a source