Activity in Android
In this tutorial you will learn about one of the most important topics related to Android app development, which is Activity.
What is an Activity in Android?
Activity is nothing but a java class in Android development, in which has some predefined methods which are triggered at different App states, where you can override to perform anything you want.
Activity class provides you with an empty method allowing us to be the controller of everything.
For example, if you have a function specified by our mind → onSeeingSomethingFunny(), although you know what happens inside this, but what if you can override and provide our own definition to this function.
@Override onSeeingSomethingFunny() { start crying; }
There can be multiple Activities in Android app, but there will be only one Main Activity. For example, In Java programming the execution of the program always start with main() method. Hence, when the user presses the App icon and the Main Activity is called and the execution starts from the onCreate() method of the Activity class.
Different States of Application (or, the main Application Activity)
Starting from a user clicking on the android App icon to launch the app and the user exiting from the App and there are certain defined states that the App is in, let’s see what they are.
- When the user clicks on the App icon and the Main Activity gets started and it creates the App’s User Interface using the layout XMLs and the App or Activity starts running and it is said to be in ACTIVE state.
- When any dialog box shows on the screen like when we press exit on some apps and it will shows a box confirming whether you want to exit or not so At that point of time, you are not able to interact with the App’s UI until we deal with that dialog box/popup and In such a situation, the Activity is said to be in PAUSED state.
- When you press the Home button while using the app, our app doesn’t close and it just get minimized so This state of the App is said to be STOPPED state.
- When you finally destroy the App i.e when we completely close it, then it is said to be in DESTROYED state.
So, all in all there are four states of an Activity(App) in Android namely, Active, Paused, Stopped and Destroyed.
From the user’s perspective So The activity is either visible and partially visible or invisible at a given point of time.
Active State
- When an Activity is in active state and it means it is active and running.
- It is showing to the user and the user is able to interact with it.
- Android runtime engine treats the Activity in this state with the top priority and never tries to kill it.
Paused State
- An app activity being in this state means that the user can still see the Activity in the background such as behind a transparent window or a dialog box that is partially showing.
- Android Runtime usually does not close an app Activity in this state but may do so in an extreme case of resource crunch.
Stopped State
- When an app Activity is started on top of the current one or when a user hits the Home key then the activity is brought to Stopped state.
- Android Runtime may close such an Activity in case of resource crunch.
Destroyed State
- When a user hits a Back key and Android Runtime decides to reclaim the memory allocated to an Activity i.e in the paused or stopped state and It goes into the Destroyed state.
- The Activity is out of memory as well as it is invisible to the user.
Activity Lifecycle methods
- When we will 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 android app needs GPS tracker for functioning or not?
Yes, surely, 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, so we coded it that whenever a user starts it, this has to ask the user to switch ON the GPS tracker, so it is required.
So, we can also tell the app to perform a few things before exiting or getting destroyed.
This is the role of Activity Lifecycle and there are six key lifecycle methods through which every Activity goes depending upon its state. They are:
- onCreate()
- onStart()
- onResume()
- onPause()
- onStop()
- onDestroy()
When the android app Activity comes back to focus from the paused state, onResume() is invoked.
When you reopen any app(after pressing Home key) and Activity now transits from stopped state to the active state. So, onStart() and onResume() methods are called.
To destroy the Activity on the screen, you can hit the Back key and This moves the Activity into the destroyed state, so During this event, onPause(), onStop() and onDestroy() methods are invoked.