Activity Lifecycle Android | Activity Lifecycle Methods | Activity Lifecycle In Android

Before Starting, If you’re not familiar with activity then first watch my previous video. I described What is Activity, Why do we use it, and How can we create it.

And if you've already seen it, then go ahead.

What is Activity Lifecycle?

The activity lifecycle is the set of states an activity can be in during its entire lifetime, from the time it is initially created to when it is destroyed.

The Activity class provides a number of callbacks to know that a state has changed: that the system is creating, stopping, or resuming an activity, or destroying the process in which the activity resides.

Why Activity Lifecycle needed?

Within the lifecycle callback methods, you can declare how your activity behaves when the user leaves and re-enters the activity.

For example, if you're building a streaming video player, you might pause the video and terminate the network connection when the user switches to another app. When the user returns, you can reconnect to the network and allow the user to resume the video from the same spot.

In other words, each callback allows you to perform specific work that's appropriate to a given change of state. Doing the right work at the right time and handling transitions properly make your app more robust and performant.

Activity Lifecycle concepts?

Each stage in the lifecycle of an activity has a corresponding callback method. When an activity changes state, the associated callback method is invoked.

To navigate transitions between stages of the activity lifecycle, the Activity class provides a core set of six callbacks:

  1. onCreate()
  2. onStart()
  3. onResume()
  4. onPause()
  5. onStop()
  6. and onDestroy().

The system invokes each of these callbacks as an activity enters a new state.

Calls when the activity is first created. This is where most initialization should go:

Create views, findViewByIds, bind data to lists, etc. This method also provides a bundle containing the previous activity’s supplied data.

Calls when the activity is becoming visible to the user, as the app prepares for the activity to enter the foreground and become interactive. This is a good place to begin drawing visual elements, running animations, etc.

Calls when the activity will start interacting with the user. This is an indicator that the activity became active and ready to receive input. It is on top of an activity stack and visible to the user.

The app stays in this state until something happens to take focus away from the app. Such as receiving a phone call, navigating to another activity, or the device screen’s turning off.

onPause ():
The system calls this method as the first indication that the user is leaving your activity. It indicates that the activity is no longer in the foreground.

When activity B is launched in front of activity A, this callback will be invoked on A. B will not be created until A's onPause() returns, so be sure to not do anything lengthy here.

Calls when you are no longer visible to the user. This is a good place to stop refreshing UI, running animations and other visual things. You will next receive either onRestart(), onDestroy(), or nothing, depending on later user’s action.

Note that this method may never be called, in low memory situations where the system does not have enough memory to keep your activity's process running after its onPause() method is called.

The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it), or because the system is temporarily destroying the activity due to a configuration change (such as device rotation).

Note: Do not count on this method being called as a place for saving data! data should be committed in either onPause() or onSaveInstanceState(Bundle), not here. This method is usually implemented to free resources like threads that are associated with an activity,

That's all Folks, if you still have any confusion, clear them by commenting below.
And Be Updated with our YouTube Channel.

Android Breakdown app
Download Android Breakdown App

Tags : activity lifecycle android, activity lifecycle android example, activity lifecycle android pdf, activity lifecycle android adalah, activity lifecycle android code, activity lifecycle android stack overflow, activity lifecycle in android tutorial point, activity fragment lifecycle android, activity lifecycle callbacks android, activity lifecycle in android interview questions, activity lifecycle android, activity lifecycle android studio, activity lifecycle android tutorial