You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android生命周期解惑:onDestroy()调用差异及面试准备建议

Understanding This Activity Lifecycle Scenario for Interviews

Hey there! Let’s break this down clearly—this scenario is super relevant for Android interviews because it digs into core fundamentals of how Android manages activity stacks and lifecycle callbacks, which interviewers love testing.

Why the Behavior Differs

First, let’s unpack what’s happening under the hood:

  • When you’re on MainActivity and close the app via the Recent Apps (overview) button, Android doesn’t immediately destroy the activity. The system prioritizes keeping processes cached in memory to let users quickly resume apps. So MainActivity will likely trigger onPause() and onStop(), but onDestroy() won’t fire unless the system later needs to reclaim memory.
  • When you navigate to SecondActivity, MainActivity gets pushed to the bottom of the task stack and enters the onStop() state. When you close the app from Recent Apps now, Android clears the entire task stack—meaning all activities in that stack (including MainActivity) are destroyed, so onDestroy() is called for both.

Why This Matters for Interviews

This scenario tests more than just memorizing lifecycle methods—it shows you understand the context of when callbacks trigger, which is critical for real-world development:

  • Debunks Common Misconceptions: Many new developers assume onDestroy() always runs when an app closes. Interviewers will ask about this to see if you know it’s not guaranteed.
  • Tests Task Stack Knowledge: The task stack is a foundational concept for Android navigation. Explaining this scenario proves you grasp how activities are managed in stacks and how system actions impact them.
  • Highlights Practical Best Practices: Interviewers want to know you can apply this knowledge to avoid bugs. For example, relying on onDestroy() to save critical data is risky—you’ll need to mention using onSaveInstanceState() for UI state or onStop() for persistent data instead.

Interview Prep Tips

To nail this in interviews:

  • Explain the "Why" First: Start with the task stack and system resource management, not just the callback differences. Interviewers care about your reasoning, not just facts.
  • Clarify the Guarantee (or Lack Thereof): Explicitly state that onDestroy() is never guaranteed to be called—Android may kill the process without invoking it if memory is low.
  • Link to Real-World Code: Give examples like: "In my projects, I avoid putting critical cleanup code in onDestroy() because of cases like this. Instead, I handle cleanup in onStop() or use ViewModel to persist state across configuration changes and background processes."
  • Prepare Follow-Up Answers: Be ready for questions like: "What happens if the system kills the process while MainActivity is in the background?" or "When would you actually use onDestroy()?"

内容的提问来源于stack exchange,提问作者askatral

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:28:54