Robin Alex Panicker

Apr 21, 20233 min

Understanding the Android Activity Lifecycle

Introduction

Android activity is an essential part of the Android application development. It represents a single screen with a user interface. An Android activity can be considered as a logical entity that plays a crucial role in Android app development. Understanding the Android activity lifecycle is essential to create robust and stable Android applications.

In this article, we will learn about the Android activity lifecycle and how it works.

The Android Activity Lifecycle

The Android activity lifecycle is a set of methods that are called when an activity transitions through various states. The Android system manages the activity lifecycle, and the developer must understand it to manage the app's resources effectively.

An activity can be in one of the following states:

  1. Active State (Running): When an activity is in the foreground and is interacting with the user, it is considered to be in the active state.

  2. Paused State: When an activity is partially visible but not in focus, it is considered to be in the paused state.

  3. Stopped State: When an activity is no longer visible on the screen, it is considered to be in the stopped state.

  4. Destroyed State: When an activity is destroyed and removed from memory, it is considered to be in the destroyed state.

The following diagram shows the Android activity lifecycle:

Understanding the Activity Lifecycle Methods

The Android activity lifecycle methods are as follows:

  1. onCreate(): This method is called when the activity is first created. It is typically used to initialize variables and set up the user interface.

  2. onStart(): This method is called when the activity becomes visible to the user.

  3. onResume(): This method is called when the activity is in the foreground and is interacting with the user.

  4. onPause(): This method is called when the activity loses focus but is still visible to the user.

  5. onStop(): This method is called when the activity is no longer visible to the user.

  6. onDestroy(): This method is called when the activity is destroyed and removed from memory.

  7. onRestart(): This method is called when the activity is stopped and then restarted again.

Kotlin Code Samples

The following Kotlin code samples demonstrate how to use the activity lifecycle methods in an Android application.

1. onCreate():

class MainActivity : AppCompatActivity() {
 

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

 
// Initialize variables and set up the user interface
 
}
 
}
 

2. onStart():

class MainActivity : AppCompatActivity() {
 

 
override fun onStart() {
 
super.onStart()
 

 
// Perform any actions when the activity becomes visible
 
}
 
}
 

3. onResume():

class MainActivity : AppCompatActivity() {
 

 
override fun onResume() {
 
super.onResume()
 

 
// Perform any actions when the activity is in the foreground and is interacting with the user
 
}
 
}
 

4. onPause():

class MainActivity : AppCompatActivity() {
 

 
override fun onPause() {
 
super.onPause()
 

 
// Perform any actions when the activity loses focus but is still visible to the user
 
}
 
}
 

5. onStop():

class MainActivity : AppCompatActivity() {
 

 
override fun onStop() {
 
super.onStop()
 

 
// Perform any actions when the activity is no longer visible to the user
 
}
 
}
 

6. onDestroy():

class MainActivity : AppCompatActivity() {
 

 
override fun onDestroy() {
 
super.onDestroy()
 

 
// Perform any actions when the activity is destroyed and removed from memory
 
}
 
}
 

7. onRestart():

class MainActivity : AppCompatActivity() {
 

 
override fun onRestart() {
 
super.onRestart()
 

 
// Perform any actions when the activity is stopped and then restarted again
 
}
 
}
 

Conclusion

In this article, we have discussed the Android activity lifecycle and the methods associated with it. By understanding the activity lifecycle, developers can create stable and robust Android applications. The Android system manages the activity lifecycle, and it is essential for developers to use the lifecycle methods to manage the app's resources effectively. By using the Kotlin code samples provided in this article, developers can implement the activity lifecycle methods in their Android applications.