Activity and Fragment Lifecycle in Android (Kotlin)

Tom
3 min readFeb 5, 2023

Android activities and fragments are the fundamental building blocks of an Android app. Understanding the activity and fragment lifecycle is crucial for building a robust, stable and scalable app.

An activity represents a single screen in an Android app and it’s lifecycle is managed by the Android framework. The activity lifecycle starts when the app is launched and ends when the user closes the app. The framework provides several callbacks that allow the app to respond to changes in the activity lifecycle, such as starting, stopping, and resuming.

Similarly, fragments represent a portion of an activity’s user interface. The fragment lifecycle is closely tied to the activity lifecycle and it allows you to create reusable UI components that can be combined to build an app’s interface.

Activity Lifecycles

onCreate(): This callback is called when the activity is first created. This is where you initialize the activity and set up any required resources.

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}

onStart(): This callback is called when the activity becomes visible to the user. You can use this method to start animations or update…

--

--