Android Activities and Lifecycle

Murat Han Acıpayam
2 min readApr 26, 2023

--

An activity is a single, focused screen in your app. It is the smallest unit of your app’s user interface. Activities are typically started by other activities, and they can also be started by the system.

When an activity is started, it goes through a series of lifecycle states. These states are:

  • Created: The activity is created and initialized.
  • Started: The activity is started and visible to the user.
  • Resumed: The activity is resumed and has focus.
  • Paused: The activity is paused and no longer has focus.
  • Stopped: The activity is stopped and not visible to the user.
  • Destroyed: The activity is destroyed and no longer exists.

You can override lifecycle methods in your activity to respond to changes in state. For example, you might override onCreate() to initialize your activity's UI, or override onPause() to save the activity's state.

Lifecycle Callbacks

Android provides a number of lifecycle callbacks that you can override in your activity. These callbacks are called at specific points in the activity’s lifecycle. For example, the onCreate() callback is called when the activity is created, and the onDestroy() callback is called when the activity is destroyed.

Here is a list of the lifecycle callbacks that you can override in your activity:

  • onCreate(): Called when the activity is created.
  • onStart(): Called when the activity is started and visible to the user.
  • onResume(): Called when the activity is resumed and has focus.
  • onPause(): Called when the activity is paused and no longer has focus.
  • onStop(): Called when the activity is stopped and not visible to the user.
  • onDestroy(): Called when the activity is destroyed and no longer exists.

Saving and Restoring State

When an activity is destroyed, its state is saved. This state is then restored when the activity is recreated. You can use the onSaveInstanceState() callback to save your activity's state.

The onSaveInstanceState() callback is called when the activity is paused or destroyed. You can use this callback to save any data that you need to preserve across a configuration change. For example, you might use this callback to save the user's current position in a list.

Navigating Between Activities

You can use the startActivity() method to start a new activity. When you start an activity, you can specify the activity's intent. The intent is an object that contains information about the activity that you want to start.

For example, you might use the following intent to start the Settings activity:

Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);

Conclusion

Activities are the building blocks of Android apps. They provide a way to organize your app’s UI and to manage the user’s interaction with your app. By understanding the activity lifecycle and lifecycle callbacks, you can write activities that are responsive and efficient.

--

--