In this tutorial we will leanr about one of the most important concept related to Android development, which is Activity.
Human mind or concious is responsible for what we feel, what we think, makes us feel pain when we are hurt(physically or emotionally), which often leads to a few tears, laughing on seeing or hearing something funny and a lot more. What happens to us in the real world physically(getting hurt, seeing, hearing etc) are intrepeted by our mind(concious or soul) and we think or operate as per.
So in a way, we can say that our body is just a physical object while what controls us through every situation is our mind(soul or concious).
In case of Android → Views, Layouts and ViewGroups are used to design the user interface, which is the physical appearence of our App. But what is the mind or soul or concious of our App? Yes, it is the Activity.
Activity is nothing but a java class in Android which has some pre-defined functions which are triggered at different App states, which we can override to perform anything we want.
Activity class provides us with empty functions allowing us to be the controller of everything.
For example, if we have a function specified by our mind → onSeeingSomethingFunny()
, although we know what happens inside this, but what if we can override and provide our own definition to this function.
@Override
onSeeingSomethingFunny()
{
start crying;
}
One thing that is different here in context to our example is, that a human is created once at birth, and is destroyed once at death, and for the time in between is controlled by the mind/soul/concious. But an Activity is responsible to create and destroy an App infinite number of times. So apart from controlling the app, Activity also controls creation, destruction and other states of the App's lifecycle.
There can be multiple Activities in Android, but there can be only one Main Activity. For example, In Java programming (or programming languages like C or C++), the execution of the program always begin with main()
method. Similarly, when the user presses the App icon, the Main Activity is called and the execution starts from the onCreate()
method of the Activity class.
Starting from a user clicking on the App icon to launch the app, to the user exiting from the App, there are certain defined states that the App is in, let's see what they are.
Hence, all in all there are four states of an Activity(App) in Android namely, Active
, Paused
, Stopped
and Destroyed
.
From the user's perspective, The activity is either visible, partially visible or invisible at a given point of time. So what happens when an Activity has one of the following visibility? We will learn about that, first let's learn about these states in detail.
Note: An Activity does not have the control over managing its own state. It just goes through state transitions either due to user interaction or due to system-generated events.
Whenever we open Google Maps app, it fetches our location through GPS. And if the GPS tracker is off, then Android will ask for your permission to switch it ON. So how the GPS tracker or Android is able to decide whether an app needs GPS tracker for functioning or not?
Yes, obviously, the App when started asks for the GPS location which is only possible if the GPS tracker in switched ON.
And how does the App knows this, because we coded it that whenever a user starts it, it has to ask the user to switch ON the GPS tracker, as it is required.
Similarly, we can also tell the app to perform a few things before exiting or getting destroyed.
This is the role of Activity Lifecycle. There are six key lifecycle methods through which every Activity goes depending upon its state. They are:
onCreate()
onStart()
onResume()
onPause()
onStop()
onDestroy()
Method | What does it do? |
---|---|
onCreate() | Whenever an Activity starts running, the first method to get executed is |
onStart() | During the execution of |
onResume() | When the Activity finally gets rendered on the screen, |
onPause() | If the activity loses its focus and is only partially visible to the user, it enters the paused state. During this transition, the |
onStop() | From the active state, if we hit the Home key, the Activity goes to the background and the Home Screen of the device is made visible. During this event, the Activity enters the stopped state. Both |
onDestroy() | When an activity is destroyed by a user or Android system, |
When the Activity comes back to focus from the paused state, onResume()
is invoked.
When we reopen any app(after pressing Home key), Activity now transits from stopped state to the active state. Thus, onStart()
and onResume()
methods are invoked.
Note: onCreate()
method is not called, as it is executed only once during the Activity life-cycle.
To destroy the Activity on the screen, we can hit the Back key. This moves the Activity into the destroyed state. During this event, onPause()
, onStop()
and onDestroy()
methods are
invoked.
Note: Whenever we change the orientation of the screen i.e from portrait to landscape or vice-versa, lifecycle methods start from the start i.e from onCreate()
method. This is because the complete spacing and other visual appearance gets changed and adjusted.