Kotlin Android App Development Course, Activity Lifecycle

Hello! In this article, we will take a closer look at one of the most important concepts in Android app development, the Activity Lifecycle. An Activity is a fundamental component that composes a screen where users interact, and its lifecycle defines how it changes as the app transitions between various states. As users use the app, the Activity goes through several states, and we will learn how to save or restore important information during this process.

What is an Activity?

An Activity is the most basic component that represents a screen where users can interact in Android. Activities allow for various tasks to be performed through different UI components, and multiple Activities can be combined to build an app. For example, a photo gallery app may have an Activity for selecting photos, an Activity for viewing photos, and a settings Activity.

Activity Lifecycle

The lifecycle of an Activity is managed through several callback methods such as onCreate(), onStart(), onResume(), onPause(), onStop(), and onDestroy(). Each method is called when the state of the Activity changes, allowing the developer to control the operation of the Activity as declared in the manifest.

1. onCreate()

This method is called when the application is first launched and typically performs UI initialization tasks. It is called only once when the Activity is created for the first time. Here, the layout is set up and data binding is usually performed.


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

2. onStart()

This method is called when the Activity starts to become visible to the user. It is invoked just before the Activity begins to interact with the user, meaning the Activity is on the screen but has not yet gained focus.


override fun onStart() {
    super.onStart()
    // Prepare tasks for the Activity to be shown to the user.
}
    

3. onResume()

This method is called when the user can interact with the Activity. You can write code here to optimize the UI and user interactions.


override fun onResume() {
    super.onResume()
    // This is called when the Activity gains focus and performs UI update tasks.
}
    

4. onPause()

This method is called when another Activity begins to be shown to the user. It is mainly used for saving data or releasing resources.


override fun onPause() {
    super.onPause()
    // Save necessary data before leaving the Activity.
}
    

5. onStop()

This method is called when the Activity is no longer visible. For example, it is called when another Activity completely obscures the screen or when the app moves to the background. Necessary resources can be released here if required.


override fun onStop() {
    super.onStop()
    // Stop UI updates and release resources as needed.
}
    

6. onDestroy()

This is the last method called when the Activity is terminated. Necessary tasks for memory cleanup can be performed here, usually when the Activity is no longer in use. It cleans up all resources.


override fun onDestroy() {
    super.onDestroy()
    // Perform Activity cleanup and resource release tasks.
}
    

Activity Lifecycle Sequence

The lifecycle states when an Activity is created and terminated can be represented in the following diagram.

Activity Lifecycle

Example of Utilizing Lifecycle Methods

Now, let’s demonstrate how to use Activity lifecycle methods with a practical example. Below is an example of a simple counter app that increments the count each time a button is clicked in the user interface, demonstrating how to save and restore data using lifecycle methods.


class CounterActivity : AppCompatActivity() {
    private var counter = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_counter)
        
        // Restore previous state
        savedInstanceState?.let {
            counter = it.getInt("counter", 0)
        }

        findViewById

Conclusion

In this article, we explored the Activity lifecycle in Android using Kotlin. Understanding and effectively utilizing the Activity lifecycle can greatly enhance app performance and improve user experience. In the next lesson, we will look at other Android components such as Services and Broadcast Receivers. Thank you!