Android Activity Lifecycle with Code Example

Sasanda Saumya
3 min readJan 6, 2025

--

What is the Android Activity Lifecycle?

An Activity is a single screen in an Android app, and the lifecycle of an Activity consists of a series of methods that allow developers to manage the app’s behavior as users interact with it. The lifecycle methods help handle transitions between different states of an Activity, ensuring proper resource management and user experience.

A simplified illustration of the activity lifecycle.
Source — https://developer.android.com

Here are the key lifecycle methods:

  1. onCreate(): Called when the Activity is created. Initialize the UI and essential components here.
  2. onStart(): Called when the Activity becomes visible to the user.
  3. onResume(): Called when the Activity starts interacting with the user.
  4. onPause(): Called when the Activity is partially obscured (e.g., another Activity is launched).
  5. onStop(): Called when the Activity is no longer visible to the user.
  6. onRestart(): Called after the Activity has been stopped and is about to be restarted.
  7. onDestroy(): Called when the Activity is being destroyed, either manually or by the system.

Here are the usage of lifecycle methods:

usage of lifecycle methods
usage of lifecycle methods
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i("AppLog","onCreate");

super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);

setContentView(R.layout.activity_main);

ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});

//initialize views and other UI related tasks

}

@Override
protected void onStart() {
super.onStart();
Log.i("AppLog","onStart");
//start animations and other visual changes
}

@Override
protected void onResume() {
super.onResume();
Log.i("AppLog","onResume");
//register listeners and start services
}

@Override
protected void onPause() {
super.onPause();
Log.i("AppLog","onPause");
//unregister listeners, save data and stop services
}

@Override
protected void onStop() {
super.onStop();
Log.i("AppLog","onStop");
//stop animations and other visual changes
}

@Override
protected void onRestart() {
super.onRestart();
Log.i("AppLog","onRestart");
//refresh data or perform any other necessary tasks
}

@Override
protected void onDestroy() {
super.onDestroy();
Log.i("AppLog","onDestroy");
//release resources and clean up data
}
}

Logs for Activity Lifecycle Events

To observe the lifecycle events, the Log.i() method is used to log messages for each lifecycle method. You can view these logs in Logcat while running the application to track the transitions between different states.

Testing the Activity Lifecycle

  1. Launch the App: Observe the sequence of onCreate(), onStart(), and onResume().
  2. Press the Home Button: Observe the onPause() and onStop() methods.
  3. Reopen the App: Observe the onRestart(), onStart(), and onResume().
  4. Close the App: Observe the onPause(), onStop(), and onDestroy() methods.

Understanding the Android Activity lifecycle is essential for managing resources and ensuring a smooth user experience. By implementing and testing the lifecycle methods, you can create applications that handle transitions effectively and minimize memory leaks. Feel free to experiment with the code and adapt it to your use case.

--

--

No responses yet