Search code examples
androidsharedpreferencesondestroyandroid-ondestroy

Removing Preference data when Application killed


In my application i am storing some SharedPreference data.

I have to clear that all stored data when application killed.

So, I have done it in my activity's onDestroy() as below :

@Override
protected void onDestroy() {

    if(isBackPressed==0){
        if(Prefrences.checkPref(MyActivity.this,MAIN_PREF)){
            Prefrences.removePref(MyActivity.this,MAIN_PREF);
            Prefrences.removePref(MyActivity.this,PREF_1);
            Prefrences.removePref(MyActivity.this,PREF_2);
            Constant.displayLogE(">>>>>>>>>>","### Prefrence removed ");
        }
        Constant.displayLogE(">>>>>>>>>>","### Destroy activity ");
    }
    finish();
    super.onDestroy();
}

Here, I have taken isBackPressed because, When onBackPressed called it calles finish() automatically and onDestroy() method calls. So, I have initialized isBackPressed to 1 inside onBackPressed() method.

It doesn't matter, I just have to remove my prefrence data when app going to be killed. But, the issue is when I killing app, onDestroy() methos not calling.

Thanks.


Solution

  • try this way.

    public class App extends Application{
    
        @Override
        public void onCreate() {
          doSomeCleanWork();
        }
    }