Clear general settings

I'm trying to make it so that you can keep the high score and I also need the user to be able to reset / delete their high score. TOAST works, but data is not deleted.

public static final String PREFS_NAME = "MyPrefsFile";
    static SharedPreferences settings;
    static SharedPreferences.Editor editor;

    // When 'back' button is pressed save the highscore to settings     
    editor = settings.edit();// Create a new editor
    editor.putInt("highscore", HighScore); // Storing integer
    editor.commit();


    // When 'Show' button is pressed
    public void showPreferences(View v) {
        int highscore = GameActivity.settings.getInt("highscore", GameActivity.HighScore);
        Toast.makeText( MainMenu.this, "Your Highscore is: " + highscore, Toast.LENGTH_LONG).show();
    }

    //When delete button is pressed
    public void clearPreferences(View V) {
        GameActivity.editor = GameActivity.settings.edit();// Create a new editor
        GameActivity.editor.clear();
        GameActivity.editor.commit();
        Toast.makeText( MainMenu.this,"Highscore has been reset",Toast.LENGTH_LONG).show();
    }

      

+3


source to share


3 answers


I believe you are just reading this wrong, use this

int highscore = GameActivity.settings.getInt("highscore", 0);

      



Please note that the second parameter is the default value, the value that is returned if the value of this key is not present in the settings.

+1


source


You can try this:

settings = getSharedPreferences("MyPrefsFile", 0);
preferences.edit().remove("highscore").commit();

      



Or you can update sharepreference with 0.

0


source


Use below to clear general settings

settings.edit().clear().commit();

      

Or use below to remove one value from settings

settings.edit().remove("highscore").commit();

      

0


source







All Articles