2

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.

Tharif
  • 13,794
  • 9
  • 55
  • 77
Jaimin Modi
  • 1,530
  • 4
  • 20
  • 72
  • 2
    call finish(); before super.onDestroy(); – Tufan Jun 01 '17 at 08:07
  • if you want the data cleared when the app is killed, then why do you store it at all? why don't you just keep it in the variables, which will be cleared when app is killed? – Vladyslav Matviienko Jun 01 '17 at 08:16
  • @Tufan not worked. I have just changed it. onDestroy() not called. logs not getting insed Android Monitor.. – Jaimin Modi Jun 01 '17 at 08:20
  • @VladMatvienko I have to get Lat and Long for particular places when user clicks on NEARME button and display map accordingly. Now, When user comes back from any other activity the map should be loaded according to previous searched location. Thats why... . .. So, have any solutions ? – Jaimin Modi Jun 01 '17 at 08:22
  • https://stackoverflow.com/questions/18361719/android-activity-ondestroy-is-not-always-called-and-if-called-only-part-of-the/18361887#18361887 – Tufan Jun 01 '17 at 08:23
  • add a debug point there and check if app is going in that condition – Tufan Jun 01 '17 at 08:24
  • https://stackoverflow.com/questions/4449955/activity-ondestroy-never-called/4449988#4449988 – Tufan Jun 01 '17 at 08:24
  • @Tufan as per above commented url : Basically, there's never a guarantee that onDestroy() will be called, and in some cases processes such as your app will be killed directly, bypassing the method call anyway. So, How can I clear my data when app is going to be killed ? – Jaimin Modi Jun 01 '17 at 08:25
  • i think in your base activity on back pressed method it will surely call – Tufan Jun 01 '17 at 08:27

1 Answers1

6

try this way.

public class App extends Application{

    @Override
    public void onCreate() {
      doSomeCleanWork();
    }
}
sanemars
  • 767
  • 5
  • 18