Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
715 views
in Technique[技术] by (71.8m points)

how to remove shared preference while application uninstall in android

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

lint warning on backup

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...