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.
Asked
Active
Viewed 308 times
0
-
What kind of other data are you talking about beside shared preferences? – Edward van Raak Mar 29 '13 at 13:57
-
Both are same. First will clear data when apps closed. and other when you want for some your activity like as button click and data will clear. :) – Rahul Rawat Mar 29 '13 at 14:00
2 Answers
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
-
Yes, just make sure you change "yourpref" to the name that you gave your shared preferences. – Edward van Raak Mar 29 '13 at 14:19
-
No you don't need to add permissions for this. The first parameter should be the string of the shared preferences. How did you call your shared preferences? What is the name? – Edward van Raak Mar 30 '13 at 12:51
-
Post what you did...show exactly how you are adding the shared preferences. – Edward van Raak Mar 30 '13 at 13:27