Search code examples
androidandroid-preferences

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.


Solution

  • 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