31

I am keeping some application meta data in SharedPreferences. Whenever I uninstall the application and reinstall it, the SharedPreferences are deleted.

Is there any way to get that to remain, so that if the user does an uninstall and reinstall, they can recover their old data?

HitOdessit
  • 7,198
  • 4
  • 36
  • 59
Victor Grazi
  • 15,563
  • 14
  • 61
  • 94

8 Answers8

25

Since Android 6.0 it has been possible to use:

<application
        android:allowBackup="true">

By setting it to true your data (sharedprefs and others) will be saved on Google cloud and restored next time the app is installed. You can read more about it here. It should be noted that the default settings is true since 6.0.

Warpzit
  • 27,966
  • 19
  • 103
  • 155
  • very interesting comment – Laurent Russier May 03 '16 at 08:31
  • 2
    Is this on by default? – Oliver Dixon Jul 22 '16 at 07:42
  • 2
    @iLoveUnicorns according to the docs Yes: https://developer.android.com/guide/topics/manifest/application-element.html – Warpzit Jul 24 '16 at 20:04
  • This answer is very important to note. Thanks, Warpzit. Solved my problem. Is there an easy way to purge the backed up data? – w3bshark Jul 26 '16 at 18:38
  • @w3bshark well since it is in shared preference you could simply update the sharedpreference main key, in other words: don't use default shared preferences... alternatively you need to clear each individual entry or update app to not allowBackup and reinstall. – Warpzit Jul 27 '16 at 05:27
  • This is the default for target SDK is 32 and higher. "For apps whose target SDK version is Android 6.0 (API level 23) and higher, devices running Android 6.0 and higher automatically back up app data to the cloud. The system performs this automatic backup for nearly all app data by default, and does so without your having to write any additional app code." – Michael Levy Aug 29 '16 at 16:17
  • What if internet access is off for my app? – partho Mar 31 '21 at 08:33
  • @partho I believe this is controlled by the system and not your app. So that doesn't matter. – Warpzit Apr 03 '21 at 10:04
21

You should add a BackupAgentHelper to your app. Together with the SharedPreferenceBackupHelper, it backups the SharedPreferences to the cloud (if the device supports it). When the app is reinstalled the data is restored.

See:

BackupAgentHelper

SharedPreferenceHelper (contains all the code you need to implement it)

general Backup guide

Gabriel Ittner
  • 1,162
  • 9
  • 22
3

I'm pretty sure SharedPreferences is always deleted along with the app. In my opinion, the best way to do this would be to write a hidden file (something like ".nameOfFile") onto the SD Card or internal memory and have that contain the preferences as well.

You should use SharedPreferences though, it's the Android standard for preference management. You could make it so that the first time your app loads, it checks the SDCard for a hidden file that would have been created last time they opened it. If the file exists, then read in those inputs and store them in the SharedPreferences, if it doesn't, then either the user deleted it or the user has never installed your app before.

This is just one way to do it, and it might not be the most efficient, but I hope it helps!

  • 2
    This is how to do it, but it doesn't have to be hidden, I would argue it *shouldn't* be hidden-- Just dump it in the /data/myapp/ directory instead, and check that location oncreate. – edthethird Mar 22 '12 at 01:53
  • Writing hidden files seems like a bad practice. If every app did this, a user would wind up with tons of hidden files that they would have a hard time getting rid of. – louielouie Mar 22 '12 at 01:53
  • @louielouie Yeah that's a good point, like I said, this was only just the first way I thought of. But you're right, and the OP should go with what edthethird said. –  Mar 22 '12 at 03:22
2

As edthethird said: best lay physical files on device external storage and read them on installation. If the file content needs to be hidden from users just perform a simple encryption/decryption process.

Eitan Schwartz
  • 463
  • 6
  • 19
2

One way to do this would be to store the user data on a server. Then when the user re-installs the app or installs the app on another device, they can "sync" their user data. That would just be a small HTTP download of the data, likely stored in JSON, which you would then parse and write into the SharedPreferences.

If you don't want to maintain your own server, you could use a cloud service like Dropbox. This is how the app 1Password Reader works.

louielouie
  • 14,881
  • 3
  • 26
  • 31
1

sharedPrefs and DBs are removed when you uninstall. You would have to write outside of the app (sd for example).

SoundsDangerous
  • 708
  • 6
  • 13
1

This is actually built-in, you just need to implement a couple of classes to enable it. Data will be backed up and linked to the user's Google account, so it will be automatically restored if they install the app on a new device, re-install, etc.

http://developer.android.com/guide/topics/data/backup.html

Nikolay Elenkov
  • 52,576
  • 10
  • 84
  • 84
  • doesn't the user have to enable this in the settings ? the user can turn backup on and off , correct ? – j2emanue Jan 22 '16 at 17:26
0

At the age of 2020, it seems to me that Shared Storage is the way to go.

Android data and file Storage overview

https://developer.android.com/training/data-storage

Shared storage of documents and files

https://developer.android.com/training/data-storage/shared/documents-files

h--n
  • 5,903
  • 4
  • 31
  • 32