Activity处于暂停状态的变化及系统销毁实例时onDestroy()调用问题
Hey there! Let's unpack your questions about Android Activity lifecycle clearly:
When your Activity enters the paused state, the system calls the onPause() lifecycle method first. At this point:
- The Activity loses focus, but it might still be partially visible on screen (for example, if a semi-transparent Activity or a dialog is placed on top of it).
- Its instance remains in memory, and all state and member variables are retained.
- If the user navigates back to this Activity, it can quickly resume via
onResume()without needing to reinitialize everything. - However, if the system is running low on memory, the paused Activity is a candidate for being killed to free up resources — though this only happens when memory is really tight.
Great question, and the answer depends on why the Activity is being destroyed:
Case 1: User-initiated or explicit destruction (like pressing back button, calling
finish()):
The system will run the full lifecycle sequence:onPause()→onStop()→onDestroy(). In this scenario, your Toast inonDestroy()will show up as expected, since the UI thread is still active during this process.Case 2: System destroys the Activity due to memory constraints:
Here's the key point: the system will NOT callonDestroy()in this scenario. When the system needs to reclaim memory, it targets background Activities (those in paused/stopped state) and may kill their hosting process entirely. Since the process is terminated abruptly, there's no chance foronDestroy()to execute — and even if there were, the Toast wouldn't display because the UI thread (and the entire process) is being shut down.
Instead, when the user returns to this Activity later, the system will create a brand new instance of it, call onCreate(), and pass the savedInstanceState bundle (if you saved state in onSaveInstanceState()) so you can restore the previous state.
内容的提问来源于stack exchange,提问作者SSP

