Android Activity Life Cycle

Android Activity Lifecycle is a collection of methods exposed within the Activity class which provides the developer a resource management framework. The resource management framework allows developers to meet the unique state management requirements of each activity and handle the resource management.

android lifecycle

This lifecycle is controlled by seven methods of android.app.Activity.class. This activity is the subclass of ContextThemeWrapper class.

android activity lifecycle methods

  • An Activity is a screen which contains the user interface.
  • User can interact with these activities to perform some task such as send an email, send messages or taking the photo.
  • The above figure shows the seven methods of an Android activity lifecycle. When you run your application, an Activity goes through the different states.
  • These seven activities describes how activity will behave at different states.
  • The main purpose of an activity is to interact with the user.
  • It is the single screen in android and is like a window or frame of Java.
  • With the help of activity, user can place all the UI components or widgets in a single screen.
Following are the seven callback methods of Android Activity Lifecycle which are called by the system,

1. OnCreate()
2. OnStart()
3. OnResume()
4. OnPause()
5. OnStop()
6. OnRestart()
7. OnDestroy()

Activity MethodsDescription
OnCreate()This method is called when activity is created. It is used to initialize the activity.

It takes a 'Bundle' parameter which is used for storing and passing state information and objects between activities.
OnStart()This method is called when activity becomes visible to the user.

It is called by the system after OnCreate() method is finished.
OnResume()This method is called when activity will start interacting with the user.

It is used to initialize fields, register listeners, bind to services etc.
OnPause()This method is called when current activity is being paused and the previous activity is being resumed.

Activity is not destroyed and not visible to the user. It is used to release resources or save application data.
OnStop()This method is called when activity is no longer visible to the user.
OnRestart()This method is called after your activity has stopped and restarting.
OnDestroy()This method is called before the activity is destroyed.

States of an Activity

Activity StateDescription
RunningThis state defines that the activity is visible and interacts with the user.
PausedThis state defines that the activity is still visible but partially hidden and the instance is running but might be killed by the system.
StoppedThis state defines that the activity is not visible and the instance is running but might be killed by the system.
KilledThis state defines that the activity has been terminated by the system call to its finish() method.