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.

  1. 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.
  2. 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.
  3. 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.
  4. 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.

Activity in active state example in Android


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.

  • Activity in paused state example Android


    Stopped State

    • 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.

    Activity in stopped state example in Android


    Destroyed State

    • 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.

    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.

    Activity Lifecycle methods

    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:

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

    Flow chart showing Activity Lifecycle in Android


    MethodWhat does it do?
    onCreate()

    Whenever an Activity starts running, the first method to get executed is onCreate(). This method is executed only once during the lifetime. If we have any instance variables in the Activity, the initialization of those variables can be done in this method. After onCreate() method, the onStart() method is executed.

    onStart()

    During the execution of onStart() method, the Activity is not yet rendered on screen but is about to become visible to the user. In this method, we can perform any operation related to UI components.

    onResume()

    When the Activity finally gets rendered on the screen, onResume() method is invoked. At this point, the Activity is in the active state and is interacting with the user.

    onPause()

    If the activity loses its focus and is only partially visible to the user, it enters the paused state. During this transition, the onPause() method is invoked. In the onPause() method, we may commit database transactions or perform light-weight processing before the Activity goes to the background.

    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 onPause() and onStop() methods are executed.

    onDestroy()

    When an activity is destroyed by a user or Android system, onDestroy() function is called.

    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.