thenewbie
thenewbie

Reputation: 805

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.

Upvotes: 3

Views: 9174

Answers (4)

DeeV
DeeV

Reputation: 36045

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.

Upvotes: 5

Lieuwe
Lieuwe

Reputation: 1840

settings.edit() creates a new editor. The commit() is done on a different editor then your clear(). You need to:

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

Upvotes: 4

Pratik Sharma
Pratik Sharma

Reputation: 13415

Try with this :

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

Thanks.

Upvotes: 6

cjk
cjk

Reputation: 46475

You don't refresh settings when you resume your first activity. Therefore it hasn't read in the new values.

Upvotes: 0

Related Questions