In my Activity, I have overrided onDestory() and just put a log call to show if this method is being called. (tested on Android 4.2.2 and 4.4.4)

protected void onDestroy() {
    Log.i(TAG, "onDestroy() was called");

When I press the back button, this method gets called ( I see the log).

I believe this should not happen unless phone gets low in memory or something. I have nothing much in the app but the MainActivity and some fragments.

Here is the log when the user is in the MainActivity and presses the bacck button:

I/MainActivity? onBackPressed() was called
I/MainActivity? onStop() was called
I/MainActivity? onDestroy() was called

Why this happens?

From the Javadoc :

Perform any final cleanup before an activity is destroyed. This can happen either because the activity is finishing (someone called finish on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing method. Note: do not count on this method being called as a place for saving data!

Related posts

Recent Viewed