Activity in Android
Activity in Android
In this tutorial we will leanr about one of the most important concept related to Android development, which is Activity.
What is an Activity in Android?
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.
Different States of App (or, the main App Activity)
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.
- When a user clicks on the App icon, 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 appears on the screen, like when you press exit on some apps, it shows a box confirming whether you want to exit or not. At that point of time, we are not able to interact with the App's UI until we deal with that dialog box/popup. In such a situation, the Activity is said to be in PAUSED state.
- When we press the Home button while using the app, our app doesn't closes. It just get minimized. This state of the App is said to be STOPPED state.
- When we finally destroy the App i.e when we completely close it, then it is said to be in DESTROYED state.
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.
Active State
- When an Activity is in active state, it means it is active and running.
- It is visible to the user and the user is able to interact with it.
- Android Runtime treats the Activity in this state with the highest priority and never tries to kill it.

Paused State
- An 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 i.e it is partially visible.
- The user cannot interact with the Activity until he/she is done with the current view.
- Android Runtime usually does not kill an Activity in this state but may do so in an extreme case of resource crunch.
- When a new Activity is started on top of the current one or when a user hits the Home key, the activity is brought to Stopped state.
- The activity in this state is invisible, but it is not destroyed.
- Android Runtime may kill such an Activity in case of resource crunch.
- When a user hits a Back key or Android Runtime decides to reclaim the memory allocated to an Activity i.e in the paused or stopped state, It goes into the Destroyed state.
- The Activity is out of the memory and it is invisible to the user.
onCreate()onStart()onResume()onPause()onStop()onDestroy()










