how to remove shared preference while application uninstall in android

痞子三分冷 提交于 2019-11-26 15:21:36

问题


I have an android application to save the login details such as user name and password via SharedPreferences thats works fine, but i need to remove all my used SharedPreferences while my application uninstall. How to do it?

SavePreferences("one ", "");
SavePreferences("two", "");
LoadPreferences();

 private void SavePreferences(String key, String value){
    sharedPreferences = getSharedPreferences("TEST", MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(key, value);
    editor.commit();
   }

 private void LoadPreferences(){
    sharedPreferences = getSharedPreferences("TEST", MODE_PRIVATE);
    String strSavedMem1 = sharedPreferences.getString("MEM1", "");
    String strSavedMem2 = sharedPreferences.getString("MEM2", "");   
   } 

I want to remove this SharedPreferences when my application uninstall.


回答1:


The problem is not with preferences. It's drastically the backup manager! .. since android-23 by default backup as a task stores app's data including preferences to cloud. Later when you uninstall then install newer version you are probably going to use restored preferences. To avoid that, just add this to your manifest (or at least to debug manifest):

<application ...
        android:allowBackup="false">
...
</application>

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

You will also see that if you run Lint under Android > Lint > Security:

It's good to mention here that the process of backup is like a blackbox .. you don't know when it starts, and period between checks ... so better for developing to disable it.

==== Update ====

You may get Manifest merger issues after setting allowbackup to false. To fix that issue add:

tools:replace="android:allowBackup"

in the application element. Credit to @shahzain-ali

Alternatively you can clear cache before uninstalling app.

I hope that may help.




回答2:


SharedPreferences is always deleted along with the app uninstall.

When you uninstall any application all the changes the application have made in your internal memory are revoked, that means your SharedPreference files, Other data files, Database file, Application gets removed automatically by the Android OS.

EDITED: 29/04/15: for >= 21 API refer @Maher Abuthraa 's answer




回答3:


Its strange but I found the solution in following way:

  1. Add xmlns:tools="http://schemas.android.com/tools" in manifest tag of Manifest.xml file
  2. Add android:allowBackup="false" in application tag of Manifest.xml file
  3. Add tools:replace="android:allowBackup" in application tag of Manifest.xml file

Manifest.xml file should looks like this.

    <?xml version="1.0" encoding="utf-8"?><!--suppress ALL -->
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        package="com.package">

        // Other code

    <application
        android:name="com.package.Application"
        android:allowBackup="false"
        android:hardwareAccelerated="true"
        android:icon="@drawable/appicon"
        android:label="@string/application_name"
        android:largeHeap="true"
        android:theme="@style/AppTheme"
        tools:replace="android:allowBackup">

        <activity
            android:name="com.package.SplashActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/application_name"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        // Other code

    </application>

    </manifest>

Its done.




回答4:


The problem is not with preferences.

use this code for fix it..........

<application
    android:allowBackup="true"
    android:fullBackupContent="false"></application>



回答5:


Setting allowBackup="false" opts an application out of both backup and restore.

 android:allowBackup="false"


来源:https://stackoverflow.com/questions/15873066/how-to-remove-shared-preference-while-application-uninstall-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!