0

I wanted to ask about the activity's lifecycle.

If an activity is going to the background, or the user pressed the home button so the application itself is not visible anymore, the onStop() is called, but not always destroyed.

1) when the above happens, what actually happens to the activity? Does it move to the backstack? or something else?

2) specifically related to the first question - when will the onDestroy() method be invoked in a case where the activity has already stopped, but the onDestroy() wasn't yet called at that moment of stopping?

3) generally about onDestory() - I know onDestroy() is called when the system doesn't have enough resources anymore, or when Android is destroying a portrait/landscape layout in order to load the other one. When else can it be called?

CodeMonkey
  • 11,196
  • 30
  • 112
  • 203
  • 1
    When user press Home button Activity go to stopped state not destroyed yet . You can resume it from Recent App stack . The other part of answer you can [read here](https://stackoverflow.com/questions/4449955/activity-ondestroy-never-called). In case `onDestroy()` is missed but `Activity` is killed it will never get called again because `Activity` will be wiped out til then . – ADM Feb 20 '19 at 11:48
  • 1
    I fail to see the point of your questions. Especially "when will the onDestroy() method be invoked" does not seem make sense. It will be called when the Activity is about to be destroyed, period. The [documentation](https://developer.android.com/guide/components/activities/activity-lifecycle) should have all you possibly need. Just stick to the specified lifecycle and avoid trying to do something 'smart' to, e.g., transfer your data from one instance to the next. This will cause problems and the users to face unexpected behavior. – JimmyB Feb 20 '19 at 11:56

1 Answers1

2

1) when the above happens, what actually happens to the activity? Does it move to the backstack? or something else?

Suppose you have pressed the home button and the current application will be moved to the background state, now the object of the current app activity will be stored in the TASK and this task has all the objects of the activity of the application. So this TASK will be in the memory and the Android System will kill this task only when there is shortage of the memory or any user manually kills the app or finish() method is called.

2) specifically related to the first question - when will the onDestroy() method be invoked in a case where the activity has already stopped, but the onDestroy() wasn't yet called at that moment of stopping?

onDestroy() will only be called in the following possibilities

  1. User manually kills the application.
  2. Android System will reclaim the memory where there is shortage.
  3. When finish() is called in the code itself.

Hope it helps.

Pranay Soni
  • 403
  • 3
  • 12