4

I have observed that when i click on clear cache in settings>apps>particular app, Shared preference data get deleted.

how to keep shared preference data even if i clear the clear the cache? is it possible? if possible means give idea about that

Sharath
  • 691
  • 8
  • 23
Android
  • 47
  • 2
  • 5
  • simply you can't avoid such behaviour - back up it online – Selvin Jun 25 '15 at 10:29
  • @Selvin i dont wanna take backup of that data, i am using that data to skip some activities in app. – Android Jun 25 '15 at 10:31
  • @Selvin facebook app does this – Android Jun 25 '15 at 10:32
  • it using Accounts API ... – Selvin Jun 25 '15 at 10:32
  • @Selvin whats that.? – Android Jun 25 '15 at 10:35
  • I didn't know that google.com stopped working ... – Selvin Jun 25 '15 at 10:36
  • Is shared preferences the only option you have chosen? There are other ways to persist data and that are not affected by clear cache use of a database is one option. You must note though that data in the database can be cleared with the *clear data* option. I am guessing retrieving this information from SP is more efficient since it's local and won't require internet lookup, the same applies to a local sqllite database – Oladipo Olasemo Jun 25 '15 at 10:54
  • I use a config file to update what is stored in app's shared preferences and use the file to regenerate the latest shared preferences data...the file is stored in the internal storage. Looks redundant but it persists the app's last config before it is cleared of cache and data. – ecle Jun 25 '15 at 12:38
  • @eee how can i do so ? please tell me – Android Jun 25 '15 at 13:35

2 Answers2

4

It is worth noting that there are three types of data clearing in Android, of which your application has no control over:

  • Clear Data
  • Clear Cache
  • Clear Defaults

The only way to have persistent data is to use the SD card, but again, users won't like to have the data on their card after the app is uninstalled or users can un-mount the SD card.

Or you can consider:

  1. Storing the data on a remote server with some kind of authentication to retrieve it
  2. Using Data Backup service
Kartheek
  • 7,104
  • 3
  • 30
  • 44
1

Shared preferences is generally used to store temporary information on a user's device. So generally it holds temporary information/data.

To store data that will survive the 'Clear Cache' action, you can store information in an sqllite database.

To do this, you need to implement a content provider that will encapsulate access to the sqllite database, it will help you store and retrieve data that will not be deleted when the cache is cleared. find more information on how to create a content provider here : http://developer.android.com/training/basics/data-storage/databases.html

Oladipo Olasemo
  • 2,010
  • 24
  • 31