0

when i press end button on my samsumsung galaxy s2 it doesn't call onDestroy method

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
Toast.makeText(context, "destroy", Toast.LENGTH_SHORT).show();

}
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
Mohamed Khalifa
  • 181
  • 2
  • 9

1 Answers1

6

Its not necessary that it would be called when the activity is finishing. onPause is sure to be called. If you read the documentation of onDestroy, it says:

Note: do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here.

If you see this question Activity OnDestroy never called? , the answer here says:

onDestroy() is called only when system is low on resources(memory, cpu time and so on) and makes a decision to kill your activity/application or when somebody calls finish() on your activity.

So, when you press that button to exit the application, its not necessary that onDestroy will be called.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • 1
    I've spent hours analyzing thread interactions because `onDestroy()` was not always called when swiping my (native) activity to death from within the task switcher. Sometimes it was called, but most of the time it didn't so I suspected a deadlock\race somewhere in my code. Quite brutal to send a SIGKILL immediately, without even _trying_ to shut an activity down elegantly. Strangely enough, using ART instead of Dalvik appears to increase the frequency of `onDestroy()` calls. First time I've spent hours trying to fix a bug that is literally a feature.. – pauluss86 Jan 31 '14 at 17:15