Android SharedPreferences after device reboot

My English is very BIG because I am Russian. :)

In my application, I use SharedPreferences to store my values. Data is stored in SharedPreferences when the application is launched, and after exiting it. And everything works fine until I reboot my device. After rebooting, I cannot use SharedPreferences , and the application does not read or write data from there. I use the getPreferences(0) function to get the settings from the application data folder. I also tried using getSharedPreferences(myPref, MODE_PRIVATE) , but the effect is the same. Saves only one solution - an application to clear data after rebooting the device.

  favoriteButton = (ImageView) findViewById(R.id.favorite_button); SharedPreferences favorite = getSharedPreferences("Favorites", MODE_PRIVATE); if(favorite.getString(""+Loader.currentVideo.getTitle()+"", "") == "true") { favoriteButton.setImageDrawable(getResources().getDrawable(R.drawable.fav_selected)); } else { favoriteButton.setImageDrawable(getResources().getDrawable(R.drawable.fav_empty)); } favoriteButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { SharedPreferences favorite = getSharedPreferences("Favorites", MODE_PRIVATE); SharedPreferences.Editor editor = favorite.edit(); if(favorite.getString(""+Loader.currentVideo.getTitle()+"", "") == "true") { favoriteButton.setImageDrawable(getResources().getDrawable(R.drawable.fav_empty)); Loader.favoriteVideos.remove(Loader.currentVideo); editor.remove(""+Loader.currentVideo.getTitle()+""); } else { favoriteButton.setImageDrawable(getResources().getDrawable(R.drawable.fav_selected)); Loader.favoriteVideos.add(Loader.currentVideo); editor.putString(""+Loader.currentVideo.getTitle()+"", "true"); } editor.commit(); } }); 
+4
source share
3 answers

Thank you all for your advice! But I solved this problem! Hope this helps someone, here is the solution.

Before requesting the value of SharedPreferences, check the availability of keys !!!

SharedPreferences sharedpreferences = getSharedPreferences ("SharedPreferences", MODE_PRIVATE); sharedpreferences.contains ("key") --- check the key!

  favoriteButton = (ImageView) findViewById(R.id.favorite_button); SharedPreferences favorite = getSharedPreferences("Favorites", MODE_PRIVATE); String tempFav = ""; if(favorite.contains(""+Loader.currentVideo.getTitle()+"")) { tempFav = favorite.getString(""+Loader.currentVideo.getTitle()+"", ""); } if(tempFav.equalsIgnoreCase("true")) { favoriteButton.setImageDrawable(getResources().getDrawable(R.drawable.fav_selected)); } else { favoriteButton.setImageDrawable(getResources().getDrawable(R.drawable.fav_empty)); } favoriteButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { SharedPreferences favorite = getSharedPreferences("Favorites", MODE_PRIVATE); SharedPreferences.Editor editor = favorite.edit(); String tempCFav = ""; if(favorite.contains(""+Loader.currentVideo.getTitle()+"")) { tempCFav = favorite.getString(""+Loader.currentVideo.getTitle()+"", ""); Log.d(Loader.currentVideo.getTitle()); Log.d(tempCFav); } if(tempCFav.equals("true")) { favoriteButton.setImageDrawable(getResources().getDrawable(R.drawable.fav_empty)); Loader.favoriteVideos.remove(Loader.currentVideo); editor.remove(""+Loader.currentVideo.getTitle()+""); } else { favoriteButton.setImageDrawable(getResources().getDrawable(R.drawable.fav_selected)); Loader.favoriteVideos.add(Loader.currentVideo); editor.putString(""+Loader.currentVideo.getTitle()+"", "true"); } editor.commit(); } }); 
+2
source

The following code should work for you to maintain shared preferences. The important part is edit.commit (); If this does not work, it is possible that the phone you are using has a different behavior, as the phone manufacturer may have changed something.

 private static final String PREFERENCES = "Preferences"; static protected SharedPreferences getSharedPreferences( Context context ) { return context.getSharedPreferences( PREFERENCES, Context.MODE_PRIVATE); } public void setString(String setting, String value) { SharedPreferences settings = getSharedPreferences( getApplicationContext() ); Editor edit = settings.edit(); edit.putString(setting, value); edit.commit(); } 
+1
source

Try using the link. It should work. I tested it on my device and worked fine. Try to understand this and make the necessary changes to your application.

0
source

All Articles