Short answer
It goes through six main stages: onCreate, onStart, onResume, onPause, onStop, and onDestroy, and it is visible and interactive at onResume.
Explanation
The View is created and the data is bound in onCreate. The Activity moves to onStart when it becomes visible to the user but without full interaction. When it reaches onResume it is at the top of the back stack and ready for interaction. When the Activity loses the foreground (such as opening another Activity or a translucent Activity over it, a system dialog such as a permission request appearing, or multi-window mode) it moves to onPause. An AlertDialog or DialogFragment inside the same Activity or pulling down the notification shade does not call onPause; then onStop is called when it is fully hidden, and finally onDestroy to release resources or when finish() is called.
Code example
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun onResume() {
super.onResume()
// Resume location or camera updates
}
override fun onPause() {
super.onPause()
// Stop sensitive operations to save battery
}
}Common mistakes
- Performing long operations or blocking the main thread inside onPause, causing an ANR and slow transition to the next screen.
- Relying on onDestroy to save sensitive user data, because the system may end the process directly without calling it when memory is low.