Activity LifeCycle
Activity LifeCycle
An activity has various stages in its lifecycle.To transit from one stage to
the other stage of the activity
Need of Activity LifeCycle
The Activity class provides a number of callbacks that allow the activity 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.
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. For example, good
implementation of the lifecycle callbacks can help ensure that your app avoids:
Activity LifeCycle
Activity LifeCycle Methods
• onCreate(): It displays the UI given in the XML file when the activity starts . It
calls the SetContentView() method.
• onStart(): when our activity is in process to be displayed this callback method is
called .And after this method onResume() callback is called.
• onResume() :The core functionality of application is written within this callback
method.When the activity is just about to be displayed this callback method is
called .Suppose if you start a new activity that hides the already ongoing activity
, onResume() is called when the activity that was hidden comes back to the view
on the screen again.
• onPause(): The code you want to be executed when app is being paused
(minimised ) should be wriiten in this callback method and when the user
resumes to the application onResume() callback method will be called again.
• onStop(): when an activity is being minimised onPause() callback is
immediately called and after a few miliseconds onStop will be
called.onStop() will stop the API calls of the application.
• onDestroy(): onDestroy callback is to be called when you need to
shutdown all the operations .
• onPause(), onStop() & onDestroy() are three callback methods which
are called while our activity is being launched