Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android shared preferences not saving

I've created an Android live wallpaper and i'm trying to let a user choose an image from their phone and apply it as a background image, but when I launch the activity that start the intent to pick the images, my shared preferences don't seem to save properly.

Below is my onCreate method of the activity I start when the users presses the preference button, and the onActivityResult which gets the path of the image on the device (all that seems to work). The println after I commit the preferences prints out nothing.

@Override public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);      Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);     photoPickerIntent.setType("image/*");     startActivityForResult(photoPickerIntent, SELECT_PICTURE); }  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {     super.onActivityResult(requestCode, resultCode, data);      if (resultCode == RESULT_OK) {         if (requestCode == SELECT_PICTURE) {             Uri selectedImageUri = data.getData();             selectedImagePath = getPath(selectedImageUri);              preferences = getApplicationContext().getSharedPreferences(PREFERENCES_NAME, 0);             preferences.edit().putString(SETTINGS_BACKGROUND_IMAGE, "okok");             preferences.edit().commit();              System.out.println("Image" + preferences.getString(SETTINGS_BACKGROUND_IMAGE, ""));         }     }      finish(); } 
like image 617
jOE Avatar asked Mar 13 '12 02:03

jOE


People also ask

How can I save my shared preferences data?

Shared Preferences allow you to save and retrieve data in the form of key,value pair. In order to use shared preferences, you have to call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.

Where is shared preference file stored Android?

Android Shared Preferences Overview Android stores Shared Preferences settings as XML file in shared_prefs folder under DATA/data/{application package} directory. The DATA folder can be obtained by calling Environment. getDataDirectory() .

What can I use instead of shared preferences in Android?

Jetpack DataStore is a new and improved data storage solution aimed at replacing SharedPreferences. Built on Kotlin coroutines and Flow, DataStore provides two different implementations: Data is stored asynchronously, consistently, and transactionally, overcoming most of the drawbacks of SharedPreferences.

Can an app have multiple shared pref files?

Yes you can maintain as many shared preference files for an app as you can.


1 Answers

From the documentation:

Create a new Editor for these preferences, through which you can make modifications to the data in the preferences and atomically commit those changes back to the SharedPreferences object.

Since that's a new Editor instance, your code should be more like this:

preferences = getApplicationContext().getSharedPreferences(PREFERENCES_NAME, 0); SharedPreferences.Editor editor = preferences.edit(); editor.putString(SETTINGS_BACKGROUND_IMAGE, "okok"); editor.apply(); 
like image 162
zrgiu Avatar answered Sep 21 '22 07:09

zrgiu