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"
    package="com.dailycoding.example">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

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

</manifest>

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 {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

}

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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/txt_hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:onClick="downloadButtonClick"/>

</androidx.constraintlayout.widget.ConstraintLayout>

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.

onCreate()

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.

onStart()

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

onResume()

After onStart callback this onResume method gets called.

onPause()

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

onStop()

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

onRestart()

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

onDestroy()

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";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "onCreate: ");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop: ");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart: ");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume: ");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause: ");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "onRestart: ");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: ");
    }
}

Conclusions

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.


References:


1 Comment

Android Activity Lifecycle - with Use Cases | Daily Coding · December 14, 2020 at 12:45 am

[…] article, what the activity is. Are you are a new guy in Android App Development? If so, you should read that article first. Because today we will be focusing on Activities lifecycle only. The Activity class or […]

Leave a Reply

Your email address will not be published. Required fields are marked *