简体   繁体   中英

Android activity lifecycle - what happens after onStop() and when onDestory() is called

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM