Sobes.tech
Junior

What is activity in a mobile application?

sobes.tech AI

Answer from AI

Activity in Android is a separate screen of an application that interacts with the user. Each activity represents a distinct UI component and has its own lifecycle. An application can contain multiple activities, which may be linked to each other.

Each activity has a lifecycle managed by the Android system:

State Description
onCreate() The activity is created. Initialization is performed.
onStart() The activity becomes visible to the user.
onResume() The activity gains focus and is ready for interaction.
onPause() The activity loses focus but is still visible. It may be partially covered.
onStop() The activity is no longer visible to the user.
onDestroy() The activity is destroyed.
onRestart() An activity in the onStop() state becomes visible again.

An example structure of an Android project with activities:

// MainActivity.java
package com.example.myapp;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); // Link to layout file

        Button nextButton = findViewById(R.id.next_button);
        nextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Transition to another activity
                Intent intent = new Intent(MainActivity.this, SecondaryActivity.class);
                startActivity(intent);
            }
        });
    }
}

// SecondaryActivity.java
package com.example.myapp;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class SecondaryActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_secondary); // Link to layout file
    }
}

The application manifest (AndroidManifest.xml) defines all activities and entry points:

<!-- AndroidManifest.xml -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.myapp">

    <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/Theme.MyApp">

        <!-- Declaration of the main activity -->
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

        <!-- Declaration of the second activity -->
        <activity android:name=".SecondaryActivity"/>

    </application>
</manifest>

Thus, activities are the building blocks of the user interface in Android applications.