Reputation: 724
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", "");
Upvotes: 0
Views: 334
Reputation: 1283
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
}
Upvotes: 2
Reputation: 1304
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();
}
Upvotes: 0
Reputation: 18670
I assume from your sample code that you use
PreferenceManager.getDefaultSharedPreferences
for reading getSharedPreferences(MyPREF_MOB, Context.MODE_PRIVATE)
for writingI think you should use the same in both cases.
Try to replace
shre1 = getSharedPreferences(MyPREF_MOB, Context.MODE_PRIVATE);
by
shre1 = PreferenceManager.getDefaultSharedPreferences(context);
Upvotes: 1
Reputation: 2417
//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();
Upvotes: 0