Does the Back button trigger a call to "onDestroy"?
Asked Answered
C

4

6

When the back button is being pressed and the current activity is being popped, will onDestroy be called? It seems that it's not called for me

Calandra answered 15/11, 2011 at 21:5 Comment(1)
Uh, don't understand the "poping" part :) nor the question for that matter. Could you elaborate your wordings again, sir?Obstruct
C
1

It seems like onStop will be called. onPause will be called also on screen lock or incoming calls etc.

It Seems the right way to handle activity pop will be onStop().

Calandra answered 15/11, 2011 at 21:8 Comment(0)
S
2

It depends. If the system is not very constrained at the moment and/or your Activity isn't taking up a whole lot of memory, onStop() will probably be called. This allows your activity to stay in memory and come back really quickly when the user switches back to it. Later on, if the user hasn't used you activity for a while and the system needs to free up some memory, then onDestroy() will be called.

That said, if the system is very memory constrained or you Activity was using a ton of memory, onDestroy() might be called as soon as you push the back button and go home.

For more on the activity life-cycle, please read the documentation here.

Speculum answered 15/11, 2011 at 21:7 Comment(1)
Can confirm "it depends": Setup: new project, log lifecycle events. Device1: calls onStop & onDestroy when navigating back. Device2: calls only onStop. However, both devices trigger onStop when pressing the home button. Frustrating!Nevsa
C
1

It seems like onStop will be called. onPause will be called also on screen lock or incoming calls etc.

It Seems the right way to handle activity pop will be onStop().

Calandra answered 15/11, 2011 at 21:8 Comment(0)
O
1

From Android Developers:

There are a few scenarios in which your activity is destroyed due to normal app behavior, such as when the user presses the Back button or your activity signals its own destruction by calling finish().

Orlon answered 11/2, 2015 at 14:49 Comment(1)
Does this mean that clicking the Back button triggers an onDestroy() to be called on the current activity?Mf
V
0

Check out this discussion on stackoverflow, answers the onDestroy question fairly well, about when it is called.

Activity OnDestroy never called?

You should put your code in the onPause and onStop, and also the onSaveInstanceState/onRestoreInstanceState.

Use the onDestroy for objects that need to be cleaned up and set to null. The memory management and garbage collection processes will periodically destroy background processes, calling onDestroy.

However, to be certain that your application releases reserved memory, take care to manually clean up your objects when they are no longer needed.

I had previously mentioned some advice about using system.gc to force onDestroy to be called.

However, system.gc cannot be trusted to force onDestroy to be called, and a developer should not rely on onDestroy being called in order to clean up memory allocation objects.

So, I have edited my answer, and removed the references to system.gc.

The article mentioned in the comment by cdeange is a good article on why it is a bad practice to rely on calling system.gc. https://mcmap.net/q/18417/-why-is-it-bad-practice-to-call-system-gc

Viceregal answered 15/11, 2011 at 21:20 Comment(2)
This is a horrible answer. You should never have to call System.gc(), and a sign you aren't doing something right.Ballroom
While rather straight up, he was quite correct System.gc() isn't guaranteed to do anything never mind trigger onDestroy. onDestroy certainly isnt responsible for any "setting of objects to null" either - if its called normal GC or sometthing else has triggered it but nothing gaurentees it is called from what I can gather.Trilobite

© 2022 - 2024 — McMap. All rights reserved.