【Kotlin/Android Studio】Activity lifecycle types and roles
What you will learn from this article
- Android Studio/Kotlin What is the lifecycle of an Activity?
- Types and roles
- Differences and processing between onCreate(), onStart(), onAttach, onPause(), onStop(), and onDestroy()
index
[open]
\ アプリをリリースしました /
References: Official Reference: The activity lifecycle
Environment
- Android Studio:Meerkat
- Kotlin:2.0.21
What is an Activity?
An Activity is a component in an Android app that represents a screen containing the user interface and application logic. It is possible to have multiple Activities in an app, but the pattern of having one Activity per app and implementing the screen with Fragments is becoming more common.
An Activity is made up of an XML-structured layout file and a file written in Kotlin (Java) that controls the behavior of the app. An Activity is generated at the same time as the application is launched.
Activity lifecycle
An activity has a lifecycle from the time it is created until it is destroyed. Various lifecycle callback methods are available so that callbacks can be received according to the state of that lifecycle.
Lifecycle types
Activity provides the following six lifecycle callback methods.
- onCreate()
- onStart()
- onResume()
- onPause()
- onStop()
- onDestroy()
The following image quoted from the official site may help you understand more clearly.
Source: Official Reference: The activity lifecycle
onCreate()
This is the callback that is called when the system first creates the Activity. It is called only once in the lifecycle, so it should contain the initial setup logic for the app.
The parameter savedInstanceState is a Bundle object that holds the previous state of the Activity. If the Activity has never existed before, it will be null.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
}
For example, you can define the UI layout, initialize member variables, manipulate widgets, and bind ViewModel in this method.
Recommended article: How to use onSaveInstanceState! Recreating an Activity
onStart()
This callback is called when the Activity becomes visible and is ready to interact with the user. At this point, you should initialize any UI-related components.
This method is called not only at the first time, but also when the app resumes after being paused.
onResume()
This callback is invoked when the Activity comes to the foreground and is ready for user interaction. It remains in this state until something interrupts or covers the Activity.
onPause()
This is called when the Activity is partially obscured or going into the background. It can occur when a dialog appears or another Activity comes in front.
As onPause() is meant to execute very briefly, it's not recommended to do heavy work like saving data, network calls, or database transactions here.
onStop()
This is the callback when the Activity is no longer visible to the user. For example, when a new full-screen Activity appears. This is a good place to release or adjust resources that are not needed while the Activity is not visible. E.g., reducing location update frequency.
onDestroy()
This is called when the Activity is destroyed, either because the user closes it or the system temporarily destroys it due to a configuration change (like screen rotation).
Detecting lifecycle changes
Recommended article: How to use LifecycleObserver
Fragment lifecycle
Recommended article: Fragment lifecycle types and roles
I'm still learning, so if you find any mistakes or improvements, I would really appreciate your feedback.
Thank you for reading.






