Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to reset all stored data store using shared preferences

Tags:

android

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

like image 638
AndroidDev Avatar asked Jul 14 '11 14:07

AndroidDev


People also ask

Does clearing cache delete shared preferences?

Clearing an app's cache does not clear the settings. The settings data (including shared preferences) is stored in a different place than the app's cache. You need to "clear data" to clear out the shared preferences.

How do I clear Android data store?

In the app's Application info menu, tap Storage and then tap Clear Cache to clear the app's cache. To clear cached data from all apps, go to Settings > Storage and tap Cached data to clear the caches of all the apps on your phone.

How is data stored in shared preferences?

Shared Preferences allow you to save and retrieve data in the form of key,value pair. In order to use shared preferences, you have to call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.

What is the syntax for deleting data in shared preferences?

To clear all the values in the shared preferences file, call the clear() method on the shared preferences editor and apply the changes.


2 Answers

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();
}
like image 108
Adil Soomro Avatar answered Sep 28 '22 02:09

Adil Soomro


its very easy..

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

and then give must

yourEditor.commit();
like image 25
Sanam Baig Avatar answered Sep 28 '22 01:09

Sanam Baig