0

Possible Duplicate:
how to use getSharedPreferences in android

I am new Android Developer. I have a query regarding Shared Preference.

How long data will be persisted in 'SharedPreference' of Android App? My requirement is to persist data for a a particular user until he log-off from the app. If same user or new user logs-in from same or new device, he/ she should have no content stored in Shared Preference from earlier session.

Please guide me to develop this. Thanks

Community
  • 1
  • 1
Ravi G
  • 859
  • 1
  • 8
  • 21

2 Answers2

1

SharedPreferences will persist data until you clear/overwrite the data by yourself or until the user uninstalls the app or you clear app's cache.

With respect to your question, when the user logs off from the app you can perform SharedPreferences.Editor.clear() followed by a commit() to remove all saved data of the current user from shared preference.

More info. here: SharedPreferences.Editor clear ()

Gagan
  • 1,497
  • 1
  • 12
  • 27
  • hi gagan. I have a query. If the user destroys the service which is storing the values by shared preference, will the value be also get destroyed?? PLease reply – Aada Mar 11 '13 at 10:46
  • If the service calls "commit" before it gets destroyed then preferences' values will NOT be destroyed and vice-versa. – Gagan Mar 11 '13 at 18:38
0

The Data which you saved in SharedPreference will store until you removed it by programmatically (changing the preference value to null).. or until you clear the history of the app or until uninstall the app ...

Sandeep P
  • 4,291
  • 2
  • 26
  • 45