6

My English very very BAD cause i'm Russian. :)

In my application I use SharedPreferences to store my values. The data has stored in the SharedPreferences, when application is running, and after exit from it. And everything works fine until I reboot my device. After reboot I can't use SharedPreferences, and the application doesn't read and write the data from there. I use the function getPreferences(0) to get preferences from application data folder. I also tried to use the getSharedPreferences(myPref, MODE_PRIVATE), but the effect is the same. Saves only one solution - data cleaning application after reboot 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();
        }
    });
BarkovAndrey
  • 131
  • 1
  • 1
  • 5
  • 1
    This is due to SharedPreferences being killed while reboot. Have a look at [data storage options](http://developer.android.com/guide/topics/data/data-storage.html). – Cdr. Powell Aug 02 '12 at 11:41
  • Please post the code you are trying. And logs if you are seeing any issue. – AAnkit Aug 02 '12 at 11:42
  • 2
    @Cdr. Powell -- SharedPreferences are stored to a file on the flash drive, and rebooting does not affect that file. – mah Aug 02 '12 at 11:45
  • 1
    BarkovAndrey - as @Ankit says, please show your code. Specifically, I'm wanting to know if you're calling the SharedPreferences commit() method, which is required to complete your changes. – mah Aug 02 '12 at 11:46
  • @cdr reboot does not affect SP.. nd barkov u must be doing something wrong... Post ur code... – AAnkit Aug 02 '12 at 13:15
  • I stand corrected. Sorry for misleading. – Cdr. Powell Aug 02 '12 at 13:20
  • I repeat, everything works fine until I reboot device or reinstall application. And helps for it - clear application data. – BarkovAndrey Aug 02 '12 at 13:31

3 Answers3

2

Thank you all for your advice! But I had solved this problem! I hope this helps someone, here's a solution.

Before you request a value SharedPreferences, check the availability of keys!!!

SharedPreferences sharedpreferences = getSharedPreferences("SharedPreferences", MODE_PRIVATE); sharedpreferences.contains("key") --- check 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();
        }
    });
BarkovAndrey
  • 131
  • 1
  • 1
  • 5
  • What if the favorite string has words like subscribe and another option is unsubscribe? .contains will return true. Please see my answer as below: http://stackoverflow.com/questions/36939380/android-persistent-checkable-menu-in-custom-widget-after-reboot-android/36998250#36998250 – iOSAndroidWindowsMobileAppsDev May 03 '16 at 07:43
0

Following code should work for you to save sharedpreferences. Important part is edit.commit(); If this doesn't work, then might be the phone that you are using has some different behavior as the phone manufacturer may have modified 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();
}
vipsy
  • 427
  • 3
  • 9
  • This is works and i can see the file with preferences in data folder when my application not run, and after reboot device i see this file! – BarkovAndrey Aug 02 '12 at 11:59
  • But only after reboot my device, my application can't see this file. – BarkovAndrey Aug 02 '12 at 12:00
  • This file continues to hold my values, but when i call getPreferences() after reboot device, can't get and write to this file. And i solve it everything after reboot - clear data my application. – BarkovAndrey Aug 02 '12 at 12:02
  • I'm sorry - my English is very very bad! ((( I'm Russian. – BarkovAndrey Aug 02 '12 at 12:04
  • Which device you use ? I think some problem with the device. Are you using any custom ROM or stock ROM ? What is android version ? – vipsy Aug 07 '12 at 06:22
0

Add these lines into manifest Application Tag.

android:allowBackup="true" android:fullBackupContent="true"

adnan javed
  • 1,338
  • 8
  • 10