19

I have created an activity where i have used shared preferences for storing data..now in another activity i have an reset button..when i click on the reset button the data store will be lost..so how that can be done..my code is

code in activity1:

    public void writeToRegister()
    {

            // Write history data to register
            SharedPreferences preferences1  = getPreferences(MODE_PRIVATE);
            SharedPreferences.Editor editor1  = preferences1.edit();
            editor1.putInt("iHistcount", CycleManager.getSingletonObject().iHistCount);
            for(int i=0;i< CycleManager.getSingletonObject().iHistCount;i++)
            {
                editor1.putLong("dtHistoryDate"+Integer.toString(i), CycleManager.getSingletonObject().dtHistory[i].getTime());

            }
            editor1.commit();
    }

    public void readFromRegister()
    {
            // Read history data from register
            SharedPreferences preferences1 = getPreferences(MODE_PRIVATE);
            CycleManager.getSingletonObject().iHistCount=preferences1.getInt("iHistcount", 0);
            for(int i=0;i< CycleManager.getSingletonObject().iHistCount;i++)
            {
                Long x=preferences1.getLong("dtHistoryDate"+Integer.toString(i), 0L);
                CycleManager.getSingletonObject().dtHistory[i]=new Date(x);
            }
    }

code for Activity 2:

Button pBtnReset = new Button(this); 
    pBtnNextMonth.setOnClickListener(pBtnReset OnClickListener);
    Button.OnClickListener pBtnReset OnClickListenernew Button.OnClickListener()
    {
                public void onClick(View arg0)
                {


                }
    };

so what i have to write in second activity reset button so that it clear the stored data

4 Answers 4

49

Get your Editor and call clear() something like this: Edit: as the user DDoSAttack mentioned. There are two ways of getting SharedPreferences

1: getting default SharedPreferences

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(con);

2: getting specific SharedPreferences

SharedPreferences prefs = Context.getSharedPreferences("FileName", Context.MODE_PRIVATE);

and here is how you'll clear it.

public void clear()
{
     SharedPreferences prefs; // here you get your prefrences by either of two methods
     Editor editor = prefs.edit();
     editor.clear();
     editor.commit();
}
2
  • @AdilSoomro What is variable con getDefaultSharedPreferences(con);? Is it Context?
    – Yohanes AI
    Sep 21, 2014 at 10:04
  • @StackOverflowError yes it is Context. Sep 21, 2014 at 11:20
4

its very easy..

yourEditor.remove(" thing you want to remove on start");

and then give must

yourEditor.commit();
4

If you want to wipe all the data in a preference file call clear() from the SharedPreferences.Editor instance

http://developer.android.com/reference/android/content/SharedPreferences.Editor.html#clear()

1
  • 2
    Also, I don't think your code works as it is written. You need to specify a filename for the SharedPreferences class. So it should look like: SharedPreferences preferences1 = getSharedPreferences("file_name", MODE_PRIVATE);
    – rf43
    Jul 14, 2011 at 14:20
3

Use SharedPreferences.Editor clear() method.

See Documentation

SharedPreferences preferences = getPreferences(0);
        SharedPreferences.Editor editor = preferences.edit();

        editor.clear();
        editor.commit();
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.