Hi there, in this article we are going to discuss Android Activity.

In Android, the Activity class is a key component of an Android app. Unlike any programming language in which apps are launched with a main() method, but the Android system initiates code in an Activity instance by invoking onCreate method from the launcher activity. Although there have some specific callback methods that are representing specific stages of its lifecycle.

This article introduces the concept of activities. However later, we will take you through some lightweight guidance about how to work with them.

The idea of Activities

As we know, the Mobile-App experience is completely different as compared to a desktop user’s experience. Because in Desktop-App, a user’s interaction doesn’t always begin in the same place. In fact, in our Mobile-App, user’s interaction always begins in the same place. For instance, If you simply launch the Play Store app, you will have a screen that contains a bunch of Apps. Right! And that screen is nothing but an Activity. Now if you tap on any one of the apps that you like, you will have another screen with some details about that particular app.

android activity
Example of multiple activities

So basically in an Android App, we could have multiple activities according to our requirements. And most apps contain multiple screens(activities) nowadays.

Activities in Android Studio

Backend And Frontend of an Activity

An activity consists of two files XML and Java/Kotlin. XML file represents the UI and Java/Kotlin file is used in order to handle the User’s interactions. All the frontend files(XML) are located at app/res/layout, on the other hand, backend files are located at app/(Java/Kotlin)/com.your_package_name. As shown in the below graphics.

Android activity
Activity’s Location

Before we go further, Let’s take a look at this area also app/Manifests. And in this AndroidMenifest.xml file, we always have to register our Activities. Android Manifest is the backbone of any Android Apps. Because it connects all the components together, not only Activities also Services, Broadcast Receivers and a few more. For additional information about the AndroidManifest file, see Android Manifest.

Exapmle of an AndroidManifest.xml

Java<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    <uses-permission android:name="android.permission.INTERNET" />


        <!--Here is how we register Activities inside the application Tag-->
        <activity android:name=".HomeActivity"/>
        <activity android:name=".MainActivity">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />


Java/Kotlin used for Back-end

So far we know Activity uses Java/Kotlin file to handle the user’s interactions such as button click, drag and drop and so on so forth. In the below code snippets, we can see a class called MainActivity whose superclass is AppCompatActivity. Therefore we have an override method called onCreate. Although there are few more methods that we can override to handle such activity states, onStart, onStop, onPause, onResume. etc.

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {


Most importantly inside that onCreate method, we must call setContentView method, by which we can refer our frontend file (XML file).

XML uses for Frontend

In this XML, we basically have to construct our UI. This is where we can design our App. Now if you pay a little attention in the below XML code snippets, you can see we are referring that above Java/Kotlin file in the root ConstraintLayout(tools:context=”.MainActivity”).

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:text="Hello World!"


There is a lot to say about an XML but for now just remember this statement is, whatever widgets or elements we want to add that widgets should have to be placed inside the root layout. For instance, in the above snippets, the root layout is ConstraintLayout and inside which we have a TextView widget.

Activities Override Methods

In general, an activity can travel through different states. And if we want to handle those states we should override these seven callbacks onCreate, onStart, onStop, onPause, onResume, onRestart and onDestroy.


This callback method gets called at the beginning when all the views or activity is created. It also has a bundle object called savedInstanceState to get the previous state of the activity.


This callback follows the onCreate method. After visible the activity.


After onStart callback this onResume method gets called.


Every time before onStop method, this one gets called. When the activity loses visibility and enters into the previous activity.


This callback is triggered when the activity is no longer visible.


This callback is triggered when the activity restarts after stopping it.


This callback is called before the activity is destroyed by the system. And this is the final callback.

Logging in Activity

Now if we override all the above methods in an activity (as shown in the below code snippets) and if we run the application through logging, we will see onCreate, onStart and onResume methods are called. And if we press the back button, onPause, onStop and onDestroy callbacks are called. For additional information, see Activity Lifecycle.

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "DailyCoding";

    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate: ");

    protected void onStop() {
        Log.d(TAG, "onStop: ");

    protected void onStart() {
        Log.d(TAG, "onStart: ");

    protected void onResume() {
        Log.d(TAG, "onResume: ");

    protected void onPause() {
        Log.d(TAG, "onPause: ");

    protected void onRestart() {
        Log.d(TAG, "onRestart: ");

    protected void onDestroy() {
        Log.d(TAG, "onDestroy: ");


Android activity is used as an entry point of an application. Once you have launched your app or activity, you can navigate to the other activities also. And all the activities are must have to register in the AndroidManifest file. So yeah! that’s all for today.

If you have any questions, don’t hesitate just ask and clear your queries. So that I can understand what kind of contents should I make. Hope you enjoyed reading this article. Thank you for being here.

android linear layout


Translate »