Activity Life Cycle in Android

  • Activity Life Cycle in Android

Understanding Activity Life Cycle in Android

Before using Android Activity in your application, It is important to  learn about the complete details of an Android Activity Lifecycle.

As a user navigates throughout an app, Android maintains the visited activities in a stack, with the currently visible activity always placed at the top of the stack.

During its lifetime, an Android activity will be in one of the following states:

  • Running – When the activity is on the foreground of the application, it is the running activity. Only one activity can be in the running state at a given time.
  • Paused – If the activity loses focus but remains visible (because a smaller activity appears on top), the activity is paused.
  • Stopped – If the activity is fully covered by another running activity, the original activity is stopped. When an activity stops, you will lose any state and will need to re-create the current state of the user interface when the activity is restarted.
  • Killed – While the activity is paused or stopped, the system can kill it if it needs to reclaim memory. The user can restart the activity.

If you’re new to Android – Start Here

Quick Look,

Activity Life Cycle methods

Android Activity Lifecycle is controlled by 7 methods of android.app.Activity class. The android Activity is the subclass of ContextThemeWrapper class.

The 7 lifecycle method of Activity describes how activity will behave at different states.

Lets have a quick look at the Activity lifecycle,

activity_lifecycle

Some important things to remember here are:

onCreate() – Called when the activity is first created. The Activity never simply rests in the Created state, it quickly moves on to the Started and Resumed states. The onCreate callback is always followed by onStart.When an Activity first call or launched then onCreate(Bundle savedInstanceState) method is responsible to create the activity. onCreate() is not required for apps. But the reason it is used in app is because that method is the best place to put initialization code.

onStart() – is called when the activity is becoming visible.This callback is normally followed by onResume but could be followed by onStop if the activity becomes hidden.If the activity is in onPause() condition i.e. not visible to user. And if user again launch the activity then onStart() method will be called.

Example : if a user was using Application A and then a notification comes and user clicked on notification and moved to Application B, in this time Application A will be paused. And again if a user again click on app icon of Application A then Application A which was stopped will again gets started.

onResume() called when activity will start interacting with the user. It is followed by onPause.

onPause() – The paused activity does not receive user input  and it cannot execute code and called when the current activity is paused and the previous activity is being resumed.

onStop() – The activity is no longer visible to the user.

onDestroy() – The onDestroy event is called whenever the activity is destroyed.The onDestroy method should clean up all the resources those were acquired by the onCreate method and previously used by the now-destroyed activity.

onRestart() – This callback is called when the activity restarts after stopping it.

  • Activity Life Cycle in Android

Activity Life Cycle in Android

Understanding Activity Life Cycle in Android

Before using Android Activity in your application, It is important to  learn about the complete details of an Android Activity Lifecycle.

As a user navigates throughout an app, Android maintains the visited activities in a stack, with the currently visible activity always placed at the top of the stack.

During its lifetime, an Android activity will be in one of the following states:

  • Running – When the activity is on the foreground of the application, it is the running activity. Only one activity can be in the running state at a given time.
  • Paused – If the activity loses focus but remains visible (because a smaller activity appears on top), the activity is paused.
  • Stopped – If the activity is fully covered by another running activity, the original activity is stopped. When an activity stops, you will lose any state and will need to re-create the current state of the user interface when the activity is restarted.
  • Killed – While the activity is paused or stopped, the system can kill it if it needs to reclaim memory. The user can restart the activity.

If you’re new to Android – Start Here

Quick Look,

Activity Life Cycle methods

Android Activity Lifecycle is controlled by 7 methods of android.app.Activity class. The android Activity is the subclass of ContextThemeWrapper class.

The 7 lifecycle method of Activity describes how activity will behave at different states.

Lets have a quick look at the Activity lifecycle,

activity_lifecycle

Some important things to remember here are:

onCreate() – Called when the activity is first created. The Activity never simply rests in the Created state, it quickly moves on to the Started and Resumed states. The onCreate callback is always followed by onStart.When an Activity first call or launched then onCreate(Bundle savedInstanceState) method is responsible to create the activity. onCreate() is not required for apps. But the reason it is used in app is because that method is the best place to put initialization code.

onStart() – is called when the activity is becoming visible.This callback is normally followed by onResume but could be followed by onStop if the activity becomes hidden.If the activity is in onPause() condition i.e. not visible to user. And if user again launch the activity then onStart() method will be called.

Example : if a user was using Application A and then a notification comes and user clicked on notification and moved to Application B, in this time Application A will be paused. And again if a user again click on app icon of Application A then Application A which was stopped will again gets started.

onResume() called when activity will start interacting with the user. It is followed by onPause.

onPause() – The paused activity does not receive user input  and it cannot execute code and called when the current activity is paused and the previous activity is being resumed.

onStop() – The activity is no longer visible to the user.

onDestroy() – The onDestroy event is called whenever the activity is destroyed.The onDestroy method should clean up all the resources those were acquired by the onCreate method and previously used by the now-destroyed activity.

onRestart() – This callback is called when the activity restarts after stopping it.