In Android applications, Activity is one of the essential components without which a user cannot interact with the application user interface (UI). This article shares detailed information about Android Activity and its fundamental concepts to help you out.

30-Seconds Summary:

  • An Android activity means a single screen with a user interface, just like a Java window or frame. It is the subclass of ContextThemeWrapper class.
  • It serves as an entry point for interacting with the user and tracks the user interactions between the system and the application.
  • When a user navigates through, out, and back to the app, the activity instances of the application transition through different states in their lifecycle.
  • There are a core set of seven callbacks to navigate the transitions between the different activity life stages in Android.
  • This Android activity lifecycle guide will cover everything you need to know about the Android Activity, how it works, different states of the activity life cycle, callback methods, and much more.

Android, an operating system that conquered more than 60% of the smartphone market, pushed developers to invest their efforts in building great apps.

The term "App" is the abbreviation for "Application," a modern word for a software application and used for a mobile app or a small piece of software that runs on a website, tablet, smartphone, or other electronic devices. Applications in Android build using either Java or Kotlin programming language.

If you would like to Enrich your career with an Android certified professional, then visit Mindmajix - A Global online training platform: “Android Training Online” Course. This course will help you to achieve excellence in this domain.

Understand the Activity Lifecycle in Android

Android - Application Components

In Android, components are the fundamental building blocks of the application. Each component is an entry point through which the user or system can enter the app.

The below listed are the main components of the Android application:

Component
Description
1. Activities
This component manages the user interaction with the smartphone screen
2. Services
This component handles the background processing associated with an application.
3. Broadcast Receivers
This component handles the interaction between Android OS and applications.
4. Content Providers
This component manages data and database management issues.

Now that you understand what Android application components are. Next, we’ll discuss the functioning of Android activity in-detail.

[ Related Article: Android Services Lists ]

What is an activity in Android?

When you open an Android application, then you see some user interface drawn over your screen. That screen is called Activity. It's the primary component of the Android application with which a user can interact to do something. An Android activity represents the single screen of the Android's apps user interface.

For example, when we open a Gmail account, we see all the emails present on the screen, and those emails are present in an activity. If we open any specific email, then some other activity opens that email.

An Android application can comprise one or more activities, meaning one or more screens, but can include only one main Activity.

If you have worked with C or C++ programming languages, you might have known that each program starts with the Main() function. Similarly, with Android, the application starts its process through Activity. First, the Main Activity is called, and then the execution starts from the onCreate() method of the Activity class. A series of methods run inside every Activity.

Subscribe to our youtube channel to get new updates..!

Any Android activity should undergo a particular lifecycle to make an Android error-free application. This is called the activity lifecycle in Android. 

Now, let's move forward and understand the concept of the Android activity lifecycle.

Android Activity Lifecycle

To understand the activity lifecycle in Android, consider the example of the Google Maps app, it collects our location details through GPS. When it is off, then Android will ask for your permission to switch it ON. 

How does the Android application's GPS tracker know this, because it's coded in such a way so that whenever a user opens it, it has to ask the user to switch ON the GPS tracker if required? Similarly, you can also tell the app to perform a few more things before exiting or getting destroyed.

This is the role of the activity lifecycle. So Android activity lifecycle means managing the state of Activity like starting, stopping, resuming, destroying, etc., and callback methods in action operate all these states.

You can override those methods and perform a particular operation to get the best output of the application.

The below diagram shows the whole lifecycle of Android Activity.

[ Related Article: Which one is Better - Android vs IOS ]

Android Activity

Each Activity has specific callbacks associated with it. The rectangle boxes represent the callback methods that help you perform operations when the Activity moves between states.

Activity lifecycle callbacks

To navigate transitions between states of the activity lifecycle, the Android supports the following methods:

Method
Description
Next
onCreate()
When a user opens an activity, the first method that gets called in is onCreate. This is where all the static work is done, like data binding to lists, creating views, etc
onStart()
onStart()
This callback is called when the Activity is ready for user interaction.
onPause()
onPause()
When the Activity does not receive user input and is unable to execute code, this callback is called. It occurs when the current Activity pauses and the previous Activity resumes
onResume() or
onStop()
 
 
onStop()
This callback is called when an activity is no longer visible to the user.
onRestart() or
onDestroy()
 
 
onRestart()
When the Activity in the stopped state is about to start again, this callback is called.
onStart()
onDestroy()
This callback is called when Activity is destroyed and removed from the memory.
nothing

So, these are the various methods that are associated with the Android activity lifecycle. Now let's see some practical scenarios where these methods and states occur:

  • When you open the application for the first time, the sequence of state change it goes through is,
onCreate -> onStart -> onResume

 After this, the Activity is ready to be used by the user.

  • When you press the home button of the app, the state changes it will go through is,
    onPause -> onStop

  • When you press the lock button when the Activity is on

    onPause -> onStop

    When you reopen the app again,

    onRestart -> onStart -> onResume
  • When you kill the app from the recent app's tray
    onPause -> onStop -> onDestroy

When we reopen the Activity now, it will call “onCreate” and not “on restart” to start the Activity.

  • After dismissing the back button
    onResume()
  • When the app is off
    onPaused() -> onStop()

These are some of the scenarios when the application goes through various states. The next section of this article provides details on the different states of the Android Activity Lifecycle.

[ Related Article: Interview Questions & Answers for Android ]

Different states of Android activity lifecycle

Right from when a user clicks on the application icon to launching, there are several states that the application undergoes within. Every Android activity goes through specific state changes in its total duration, namely Running, Paused, Stopped, and Killing. From the user's perspective, the Activity will be either visible or invisible at a given point in time.

Activity State
Description
Running
Activity in this state is visible to the user.
Paused
Activity in this state means a user can still see the background activity but cannot interact with it.
Stopped
Activity in this state is invisible but not destroyed.
Killing
The Activity is out of the memory and invisible to the user.

[ Related Article: Overview of Android ]

What is Logcat in Android?

Logcat is a command-line tool that contains a log of system messages, including stack traces when the device adds an error and messages that you have written from the application with the Log class.

Example of Android Activity Lifecycle Implementation.

Step 1: First, you need to build an Android application using Kotlin or Java. 

Step 2: After building the application, next configure the MainActivity.Java file and override the callbacks methods.

package example.activitycycle;  
  
import Android.app.Activity;  
import Android.os.Bundle;  
import Android.util.Log;  
  
public class MainActivity extends Activity {  
    /** Called when the activity is first created. */ 
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        Log.d("msg","Invoke onCreate");  
    } 
    /** Called when the activity is about to become visible. */ 
    @Override  
    protected void onStart() {  
        super.onStart();  
        Log.d("msg","Invoke onStart");  
 }  
    /** Called when the activity has become visible. */
    @Override  
    protected void onResume() {  
        super.onResume();  
        Log.d("msg","Invoke onResume");  
    }  
    /** Called when another activity is taking focus. */
    @Override  
    protected void onPause() {  
        super.onPause();  
        Log.d("msg","Invoke onPause");  
    }  
    /** Called when the activity is no longer visible. */
    @Override  
    protected void onStop() {  
        super.onStop();  
        Log.d("msg","Invoke onStop");  
    }  
   /** Called when the activity in the stopped state is about to start again. */
    @Override  
    protected void onRestart() {  
        super.onRestart();  
        Log.d("msg","Invoke onRestart");  
    }  
    /** Called just before the activity is destroyed. */
    @Override  
    protected void onDestroy() {  
        super.onDestroy();  
        Log.d("msg","Invoke onDestroy");  
    }  
}  

Basically, in the above example, we are overriding all the methods and generating the messages in LogCat. Whenever an application undergoes different states, it invokes and displays the methods in the app itself. That's how the Android activity life cycle works.

Conclusion

This brings us to the end of this article on the "Android Activity Life Cycle." We hope the information shared here is clear and added some value to your knowledge.