Question

Hello I have an app that I'm developing and I want to clear all of its data when its getting close. I don't mind if it will need to add permission or something like this. also I need it will clear all of my shared preferences.

Was it helpful?

Solution

you can use this code for delete all data from sharedpreferences.

this is for clear data when apps close:-

 @Override
public void onDestroy() {
super.onDestroy(); 
SharedPreferences preferences = getSharedPreferences("your prefre name", 0);
 preferences.edit().clear().commit();
}

this is when you want to clear all data by any your activity like as button clicked:-

    SharedPreferences preferences = getSharedPreferences("your prefre name", 0);
    preferences.edit().clear().commit(); 

it should solve your query.

OTHER TIPS

You can clear the shared preferences like this.

SharedPreferences preferences = getSharedPreferences("yourpref", 0);
preferences.edit().clear().commit();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top