2

I searched in google. i can find an answer for clearing the shared preference but i need to clear the shared preferences when app getting closed. is this possible pls help.

code clear shared preference

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
SharedPreferences preferences = 
PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("UserName", "Tonny");
editor.putInt("UserAge", 20);
editor.commit();
Sivanantham M
  • 91
  • 1
  • 8
  • 6
    Then why are you using `SharedPreferences` in the first place? The **point** behind `SharedPreferences` is to persist across uses of your app. – CommonsWare Mar 13 '15 at 11:44
  • There's no deterministic way to detect when your app is closed. If you have a single activity, you can do it on the onDestroy() method (which again isn't going to protect you from crashes, etc.). Maybe you should reconsider why you need to do this and see if you REALLY need to do it? – kha Mar 13 '15 at 11:46
  • Is there any other concepts instead of SharedPrefences @CommonsWare – Sivanantham M Mar 13 '15 at 11:50
  • how is your stack is behaving,when did you closing your app? – learner Mar 13 '15 at 11:52
  • There are many "other concepts instead of `SharedPreferences`". However, since we do not know what you are trying to do, we cannot readily help you with those "concepts". In general, if you have data that you want to only use while your process is in memory, you just hold onto that data in memory, not in a file. – CommonsWare Mar 13 '15 at 11:53
  • When i close my app my SharedPrefences is not getting deleted. – Sivanantham M Mar 13 '15 at 11:55
  • Of course not - it is intended **not** to be. Before you can do anything that might work, you need to **define exactly what you mean by closing an app** - this is difficult, **because the concept is foreign to android** and may not at a technical level match with what you think you see as a user. what you really should do is **revise your requirement to one that fits with android** such as cleaning up on the next run. – Chris Stratton Mar 13 '15 at 12:11

2 Answers2

6

It would be possible to clear SharedPreferences every time, either when the main activity is destroyed or when the app is next run. However, as @CommonsWare points out, doing this would miss the whole point of SharedPreferences. (And check out @CommonsWare's reputation to see whether or not to believe them.) The purpose of SharedPreferences is to store values to use the next time the app is run. If you clear them automatically every time...

You can just use a variable to store your data. This variable will naturally be cleared every time the app closes.

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • I am also storing my data in a variable but there is a problem that when the Android silently kills the app to reclaim some memory, then how can i refetch my data because if i were using `shared prefrences` then my problem will get solved. – Sudhanshu Gaur Apr 06 '17 at 11:45
  • @SudhanshuGaur, if you want to save a variable between two different runs of your app, then `sharedPreferences` would be an appropriate place to do that. – Suragch Apr 06 '17 at 13:22
  • i wasn't talking about two different runs i was talking about suppose, user uses my app and then press home button and after 3 hours he click on recents apps and from there open my app again but now as you know there can be a case when android can flush the memory possessing by my variable which i was storing in my activity so here without shared prefrenc how can i solve this case ?? P.S I only want to use my variable till uses closes my app because next time i will reinitialise this variable again ?? – Sudhanshu Gaur Apr 06 '17 at 14:06
  • no you are not getting, i mean after three hours user doesn't restarts the app he opens my app by clicking on recents apps, now the problem is suppose i am storing a variable in my Application class now suppose i need this variable value in my activity class B and user pressed home button when he was on activity B, now when suppose in the 3 hours time android kills my Application class so now when user navigate back to my app from recents app now value of my variable will be null and when i will try to access that value again in my Actviity B class my code will crash. – Sudhanshu Gaur Apr 06 '17 at 14:36
  • because when user closes the app i want to clear all data because it's a sensitive data. – Sudhanshu Gaur Apr 06 '17 at 15:29
  • @SudhanshuGaur, whether the user is closing your app (whatever that means) or the system is killing it, the result is the same. Your variables are lost. If you want to keep them, then you must put them in some sort of [long term storage](https://developer.android.com/training/articles/security-tips.html#StoringData). If the data is sensitive, then you need to [take that into account](http://stackoverflow.com/q/8184492). If you don't want to store the data in any form, then another option is to recreate the data (perhaps by asking the user to reenter it). – Suragch Apr 07 '17 at 00:18
-1

Better to go with other options instead of Shared Preferences.

Using Application class to store the data is one of the options, through which you can achieve your requirement.

suresh n
  • 335
  • 1
  • 4
  • 14
  • This would be ephemeral to a degree, but not necessarily one corresponding to the desired behavior - it can hang around between what the user may think of as sessions, and it can be killed and recreated during what the user things of as the same run. – Chris Stratton Mar 13 '15 at 12:13