2

When an automatic update occurs, the old date save in the preferences for my app gets delete.
How can i prevent this deletion?
I want that when my app auto updates , it does not delete my old data saved.

Noman
  • 4,049
  • 10
  • 38
  • 59

2 Answers2

1

I am not sure whether this will be a good solution but just a suggestion you can try.

step 1: whenever you changeg (add/edit/remove) your data store it in permanent storage, you may try any of the following

 a. save the data in file in sd card
 b. store the data to your remote server or 
 c. store in internal memory of the phone. 

(I am not sure whether it will persists after update at case c, for reference can check here

Step 2: creae a BroadcastReceiver that listens to the ACTION_PACKAGE_REPLACED Intent. So you know when your application package is updated. NOw read the data again from the storage where you saved the data ( either 1a/1b/1c)

Caution: It is not a good thing to save user data without his concern.

stinepike
  • 54,068
  • 14
  • 92
  • 112
  • Thanks for a detail reply StinePike... I am saving the data in shared prefs already but now i wanna ask that does data gets deleted on auto update ?? According to my R&D, the data is saved with the package name so auto update shall not change it.. – Noman Mar 12 '13 at 13:09
  • I knew that SharedPreference are stored even after updating .. but according to this answer http://stackoverflow.com/a/10201011/931982 it may be delete sometimes. – stinepike Mar 12 '13 at 13:15
  • but in every other places I found that SharedPreferences preserves data except cleardata or uninstall. But as you told that your data from preference lost I thought You have already tried SharedPreference – stinepike Mar 12 '13 at 13:17
  • ok.. yeah i am saving my data in shared preferences. Thanks for the info man – Noman Mar 12 '13 at 13:32
1

Android save your SharedPreference under /data/data/your package name/shared_prefs

Generally, update application won't delete your sharedPreference.

private void replaceNonSystemPackageLI(PackageParser.Package deletedPackage,
        PackageParser.Package pkg, int parseFlags, int scanMode, UserHandle user,
        String installerPackageName, PackageInstalledInfo res) {
    ...
    // First delete the existing package while retaining the data directory
    if (!deletePackageLI(pkgName, null, true, PackageManager.DELETE_KEEP_DATA,
            res.removedInfo, true)) {
        // If the existing package wasn't successfully deleted
        res.returnCode = PackageManager.INSTALL_FAILED_REPLACE_COULDNT_DELETE;
        deletedPkg = false;
    } else {
        ....
    }
    ....
}

I think you need to check the following:

  1. Is your device a rooted device? Apps on a rooted device can delete anything they want.

  2. Did you use a different package name in that updated apk?

StarPinkER
  • 14,081
  • 7
  • 55
  • 81
  • No my device is not rooted and my package name is same for each update. I just got my data deleted once because of any unknown reason but i guess each time it is not getting deleted. I will check this issue precisely on the next update and if it happen again then surely i will look for your proposed solution. Thanks for the help. – Noman Mar 12 '13 at 13:44
  • np, this is an unexpected behavior, so maybe you need to provde more information about your app or your device for a more helpful solution. :) – StarPinkER Mar 12 '13 at 13:52
  • What about /data/data/PKGNAME/files/ directory? Will that survive a package update as well? Thx. – Bram Aug 14 '13 at 19:44
  • I haven't checked the code, but I think that will survive during a update. @Bram – StarPinkER Aug 20 '13 at 13:29