0

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.

dorbt12
  • 139
  • 2
  • 10

2 Answers2

1

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.

Rahul Rawat
  • 999
  • 2
  • 17
  • 40
  • I tried the code you sent to me and it didn't work could you explain more about what each part do and if it will clear all of my data i have saved on it or only part of it? – dorbt12 Mar 29 '13 at 14:16
0

You can clear the shared preferences like this.

SharedPreferences preferences = getSharedPreferences("yourpref", 0);
preferences.edit().clear().commit();
Edward van Raak
  • 4,841
  • 3
  • 24
  • 38