Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android not clearing sharedpreference

I want to clear all the sharedpref i put on my first activity and delete it on 2nd activity. But the clear function is not working.. here is my code

public class MyActivity extends Activity implements View.OnClickListener {
    /**
     * Called when the activity is first created.
     */

    public static final String PREFS_NAME = "ResumePrefs";

    SharedPreferences settings;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ((Button)findViewById(R.id.button)).setOnClickListener(this);

    }

    @Override
    protected void onResume() {
        try{
            Toast. makeText(this,settings.getString("answer_id",""),Toast.LENGTH_LONG).show();
        }catch (Exception e){
            Toast.makeText(this,e.toString(),Toast.LENGTH_LONG).show();
        }
        super.onResume();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button:
                 settings = getSharedPreferences(PREFS_NAME, 1);
                SharedPreferences.Editor editor = settings.edit();
                editor.putString("answer_id", "This is a test");
                editor.commit();
                     Intent intent = new Intent(this,destroyPref.class);
                startActivity(intent);
                break;
        }
    }
}

and my2nd activty:

public class destroyPref extends Activity {
    public static final String PREFS_NAME = "ResumePrefs";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.destroy);

    }

    @Override
    public void onBackPressed() {
        SharedPreferences settings = this.getSharedPreferences(PREFS_NAME,1);
        settings.edit().clear();
        settings.edit().commit();
        super.onBackPressed();
    }

    @Override
    protected void onResume() {
        SharedPreferences settings = this.getSharedPreferences(PREFS_NAME,1);
        settings.edit().clear();
        settings.edit().commit();
        super.onResume();
    }
}

Once i pressed the back button i will go back to the main activity and expect that the preference value is cleared, unless i pressed again the button. I tried bot backpress and resume but still the sharedpref value is still there.. Am i missing something? Thanks.

like image 772
thenewbie Avatar asked Dec 07 '22 09:12

thenewbie


2 Answers

Try with this :

SharedPreferences.Editor editor = settings.edit();
editor.clear();
editor.commit();

Thanks.

like image 50
Pratik Sharma Avatar answered Dec 25 '22 12:12

Pratik Sharma


The issue you're facing here is that with the code:

   SharedPreferences settings = this.getSharedPreferences(PREFS_NAME,1);
   settings.edit().clear();
   settings.edit().commit();

You are creating two editors. One is set to clear, but you never commit it so it doesn't clear. The other commits, but you never set it to do anything.

   SharedPreferences settings = this.getSharedPreferences(PREFS_NAME,1);
   settings.edit().clear().commit();

This will create one editor, set it to clear, then commit the changes. I am pretty sure that the values will be nulled out when you return. I don't honestly know though so please post the results and I'll edit this answer.

like image 45
DeeV Avatar answered Dec 25 '22 13:12

DeeV