简体   繁体   中英

android sharedpreferences does not save data when app restart

I have a sharedpreference which save data fine until the app close. when app restart the data in sharedpreferences was gone please tell me the solution. I want when app close or restart or phone restart my data in preferences was saved.

public static final String MyPREF_MOB = "MyPref_mob";
public static final String WALLPAPER_MOB = "wallpaper_mob";

if(encodedImagee!=null) {
    // shre1 = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor edit = shre1.edit();
    edit.putString(WALLPAPER_MOB, encodedImagee);
    edit.apply();
    //  Toast.makeText(Profile1Activity.this, "new same image in prefrences", Toast.LENGTH_SHORT).show();
}
shre1 = getSharedPreferences(MyPREF_MOB, Context.MODE_PRIVATE);
final String image_save =shre1.getString(WALLPAPER_MOB, "");

i have also try this

 if(encodedImagee!=null)
        {
           // shre1 = PreferenceManager.getDefaultSharedPreferences(context);
            SharedPreferences.Editor edit = shre1.edit();
            edit.putString("mob_wallpaper", encodedImagee);
            edit.commit();
            //  Toast.makeText(Profile1Activity.this, "new same image in prefrences", Toast.LENGTH_SHORT).show();
        }
        shre1 = PreferenceManager.getDefaultSharedPreferences(context);
        final String image_save =shre1.getString("mob_wallpaper", "");

i have use this code data save when app restart also i can get data but when phone restart data again gone

if(encodedImagee!=null)
        {
           // shre1 = PreferenceManager.getDefaultSharedPreferences(context);
            SharedPreferences.Editor edit = shre1.edit();
            edit.remove("mob_wallpaper");
            edit.apply();
            edit.putString("mob_wallpaper", encodedImagee);
            edit.apply();
            //  Toast.makeText(Profile1Activity.this, "new same image in prefrences", Toast.LENGTH_SHORT).show();
        }
        shre1 = PreferenceManager.getDefaultSharedPreferences(context);
        final String image_save =shre1.getString("mob_wallpaper", "");

Try this,

        SharedPreferences sharedPreferences = getSharedPreferences("preference_name", Context.MODE_PRIVATE);
        if (encodedImagee != null){
            SharedPreferences.Editor edit = sharedPreferences.edit();
            edit.putString("mob_wallpaper", encodedImagee);
            edit.commit();
        }else{
            final String image_save = sharedPreferences.getString("mob_wallpaper","");
            // put your logic over here
        }

I assume from your sample code that you use

  • PreferenceManager.getDefaultSharedPreferences for reading
  • getSharedPreferences(MyPREF_MOB, Context.MODE_PRIVATE) for writing

I think you should use the same in both cases.

Try to replace

shre1 = getSharedPreferences(MyPREF_MOB, Context.MODE_PRIVATE);

by

shre1 = PreferenceManager.getDefaultSharedPreferences(context);
//get the preference instance with this
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
// acccess a saved key value
mPrefs.getString("key", "default_value");
//save a key-value
mPrefs.edit().putString("key", "new_value").apply();

modify your code in this way:

@Override
public void onCreate(.....){
    super.onCreate(......);
setContentView(R.layout.your_layout_xml);
shre1 = getSharedPreferences(MyPREF_MOB, Context.MODE_PRIVATE);

final String image_save =shre1.getString(WALLPAPER_MOB, "");

and when you want to update your image

if(encodedImagee!=null) {
// shre1 = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor edit = shre1.edit();
edit.putString(WALLPAPER_MOB, encodedImagee);
edit.commit(); //make use of commit
//  Toast.makeText(Profile1Activity.this, "new same image in prefrences", Toast.LENGTH_SHORT).show();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM