Read More

Application restored some data in Shared Preference even after reinstall

Have you ever wondered that despite uninstalling your application there is some data in your application when you reinstall it?

All we think is that the data was somewhere in shared preference and it was not deleted when we uninstalled the application.But that is simply not the case.

The shared preference is definitely deleted when the application is uninstalled. So what is the cause for this and how do we rectify it.

CAUSE : 

The Reason for this unwanted data is not the Shared Preference but is the “BackupManager”.

Since Android API 23 ( or Android “6.0” or Android “Marshmellow”) the BackupManager stores all the data of an application including the Shared preference on the cloud. Thus when your application is reinstalled, the shared preference data is restored.

A key point here to note is that the process of backup is like a black-box. We cannot know when it started, how it started and when will next cycle of backup occur. So if you are thinking of disabling backup process, or to set the shared preference data just before backup that is perhaps not possible for now.

Another key point to note is that when we create an application in android, in the manifest file the key “allowBackup” is set to true by default.

SOLUTIONS :

Solution 1: Your App Should warn the user to disable the Automatic Restore feature on the Android device. For this, the user needs to go to the path
Settings–> Backup & Reset–>Automatic restore  and then turn the switch off.
But this is not recommended, as this will turn off the automatic backup feature for all the applications installed on the device.

Solution 2: Go to your Manifest File. In the application tag, find the attribute “allowBackup” and set it to false.

On the precautionary note, you can also check for keys “fullBackupContent” and “fullBackupOnly”. and set them to false.

And “tools:replace” =”android:allowBackup” add this line too.
It is Done.

CODE :

For further help regarding this, you can check the official document here.
https://developer.android.com/guide/topics/data/autobackup.html

That’s all for now.

We can also explicitly set the rules for what data should be included in the auto backup, which we will see some another time.
Also, we can externally set up a backupAgent and store the data we want to save after disabling the automatic BackUp process, which we will also learn some other time.

Keep coding and Keep Sharing 🙂

 

. . .

Comments (6)

Add Your Comment

  • Muhammad Zeeshan Aslam
    Thanks for sharing this.
  • Yasir Tanveer
    Thanks
  • Adam
    Hey bro i have added this part of code in manifest and then when i run the app it’s shows an error “Manifest merger failed with multiple errors, see logs” like when i add this part of code then i face this problem so it’s mean by changing the value of allowbackup = “false” it shows an error right? so how do i resolve it?
    • anchit
      just add this line in your manifest file under application tag –>
      tools:replace=”allowBackup”
  • css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close