Skip to content
tezvyn:

Trace an Activity's lifecycle when a user navigates away and returns

Source: developer.android.comEasyHow cards are made

Trace an Activity's lifecycle when a user navigates away and returns

This tests your understanding of resource management and state preservation, not just memorization. A great answer traces onPause -> onStop when leaving, and onRestart -> onStart -> onResume when returning, explaining what happens in each.

What's really being asked

This question tests your understanding of resource management, not just rote memorization. The interviewer wants to know if you can build a stable, battery-efficient app. They are listening for your ability to connect lifecycle methods to concrete actions, like releasing a camera in onStop or pausing an animation in onPause. It's a fundamental concept that separates developers who write apps that work from those who write apps that work well and don't crash.

The full answer

A strong answer walks through the specific sequence and explains the purpose of each step. First, the 'away' path: onPause() is called when the Activity is partially obscured (e.g., by a dialog). This is for pausing UI updates or animations. Then, onStop() is called when the Activity is no longer visible. This is the signal to release heavy resources like network connections, sensors, or a video player. The system may also call onSaveInstanceState() around this time to preserve UI state. Second, the 'return' path: onRestart() is called, indicating the Activity is coming back from a stopped state. This is followed by onStart(), where you should re-acquire resources released in onStop(). Finally, onResume() is called right before the Activity becomes interactive again, where you resume anything paused in onPause().

The mistakes people make

The most common mistake is simply listing the methods without explaining their purpose. A major red flag is forgetting onRestart() in the return sequence; many candidates incorrectly jump from a stopped state directly to onStart(). Another error is confusing the roles of onPause() and onStop(). A senior candidate should know that heavy resource cleanup belongs in onStop(), not onPause(), because onPause() execution needs to be extremely fast. Finally, treating onSaveInstanceState as a general-purpose persistence mechanism is wrong; it's for small amounts of transient UI state to survive configuration changes and process death, not for saving user data permanently.

What usually comes next

Expect follow-ups like: "When might onDestroy() not be called?" (Answer: When the system kills the process for memory). "How does a ViewModel change how you handle this?" (Answer: It survives configuration changes, simplifying state management). "What's the difference in lifecycle between launching a new Activity versus pressing the home button?" (Answer: Home button calls onStop, a new Activity might just call onPause if it's translucent).

A concrete example

In a video streaming app, when the user navigates away, onPause() should pause the video playback (player.pause()). Then, onStop() must fully release the video player and its associated decoders (player.release()), as these are very resource-intensive. When the user returns, onRestart() is called, followed by onStart(), where you would re-initialize the player. Finally, onResume() would be the place to call player.play() to resume playback from where it was paused.

Interview question

When a user navigates away from an Activity, why is it better to release a resource-intensive object like a camera in onStop() instead of onPause()?

  • a.Resources released in onPause() can only be reacquired by restarting the Activity with onCreate().
  • b.onStop() is the last reliable callback, as onDestroy() is not guaranteed to be called.
  • c.The system may kill the process for memory after onPause() runs, so cleanup must be fast.
  • d.onPause() execution must be very fast because it blocks the next Activity from appearing.Correct
Why?

Correct. onPause() must be lightweight because it blocks the UI thread and can delay the next Activity from appearing. Heavy cleanup belongs in onStop() since the Activity is already hidden from the user.

Just read this? Test yourself on what you have been reading.

Read the original → developer.android.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.

See open roles