Android – ny way to capture the Clear Data action programmatically in application

android

I developed one android app which is having sets of activities and one background service running. I am keeping some app data to the device's internal memory so when users clicks on Clear Data option from Settings->Applications-> Clear Data button, all data saved in internal memory gets cleared.
I have to captured Clear Data click event or action(if any available) when user clicks on Clear Data button in my App's service OR in broadcast receiver class, can anyone suggest that is there any action thrown by device upon clicking on Clear data of app so I could catch that action in my app broadcast receiver class and perform desired task???

Please provide me the resolution with some example.

Regards,
Piks

Best Answer

The best way would be storing a value in SharedPreferences like "IS_DATA_CLEARED" when the app is run first or when you save all required details and always make sure this value is never fiddled with at any time during the app's life cycle.

SharedPreferences appSharedPrefs = PreferenceManager
            .getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = appSharedPrefs.edit();
editor.putBoolean("IS_DATA_CLEARED",false);
editor.apply();

Then every time do the below check.

SharedPreferences sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(this);
boolean IS_DATA_CLEARED =  appSharedPrefs.getBoolean("IS_DATA_CLEARED",true);

if(IS_DATA_CLEARED){
  //DATA IS CLEARED
}
else{
  //DATA IS NOT CLEARED
}
Related Topic