4

I appear to have the opposite issue to every other post on the topic I have come across. Others find that onDestroy() is not always called but I find it always and immediately is.

I am checking whether it is called with the following:

@Override
protected void onDestroy() {
    super.onDestroy();
    Log.d(TAG, "onDestroy");
}

This log code is triggered immediately after either clicking back or using my actionbar button which just calls the home activity as an intent.

I have looked around but everyone says the lifecycle should look like this: http://developer.android.com/images/activity_lifecycle.png

This behaviour is not limited to a single app; I have tried downloading some example code and adding in the above debugging code only to find the same behaviour. It is also not limited to a single android version; I have tried 2.1 and 2.3 in the emulator and also 4.0.2 on a physical device.

Any ideas of what I might be doing wrong?

dhewett
  • 43
  • 1
  • 4

1 Answers1

8

pressing back key actually provokes finish() method on your activity, and it causes your activity to be paused->stopped->destroyed

so technically this behavior is functional as expected.

waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • Other [posts](http://stackoverflow.com/questions/4778754/kill-activity-on-back-button) seem to dispute that, others have used extra code to call finish inside onPause() to make sure their activity is destroyed. Regardless, I find onDestroyed() is called even if the home activity is called, I believe this corresponds to "Another activity comes into the foreground" on the lifecycle diagram http://developer.android.com/images/activity_lifecycle.png – dhewett Feb 23 '12 at 18:37
  • 1
    no, this is how the activity lie cycle actually works. If you press **Home** key then onDestroy may not get called (depends if phone runs on low memory), but onStop will. And if you press **Back** key then onDestroy gets called. I think you should create a test app with an activity and log messages on each on-event to observe deeply – waqaslam Feb 23 '12 at 21:27
  • Thanks for your help clearing that up Waqas, you are indeed correct. The wording in the android documentation led me to think onDestroyed() should only be called if memory is low but it is supposed to be called on Back key press. [This](http://qwapp.blogspot.com/2011/01/android-app-life-cycle-and-home-and.html) blog post explains it. – dhewett Feb 24 '12 at 05:34
  • Hello @waqaslam. Does onDestroy() not stop the app from running in background?. Because if it does then after onDetroy() is called I still see my app in "running application" list. – Jaikrat Oct 04 '15 at 17:14
  • 1
    @Jaikrat ´onDestroy´ only guarantees that the `Activity` is finished gracefully. It does not destroy the application context, which is the reason that the app appears in running applications. The application context is destroyed by the system itself (unless force-stop by user) when it thinks it need to free-up resources. – waqaslam Oct 04 '15 at 17:25