Understanding Android Activities and Intents

Tutorial 2 of 5

Introduction

This tutorial aims to help you understand the fundamental concepts of Android development, namely Activities and Intents. Activities in Android represent the user interface (UI) screens, while Intents are used for navigating and exchanging data between these Activities.

By the end of this tutorial, you will learn:
- What Android Activities and Intents are
- How to create and use Activities and Intents in your Android app

Prerequisites:
- Basic understanding of Java or Kotlin programming language
- Basic understanding of Android Studio

Step-by-Step Guide

1. Understanding Android Activities

An Activity in Android is a single screen with a user interface. For example, an email application might have one Activity that shows a list of emails, another Activity to compose an email, and another Activity to read emails.

In Android, you define your Activity in two places:
- The Activity Java file (or Kotlin file). This is where you write the code for what your Activity will do.
- The layout XML file. This is where you define what your Activity will look like.

2. Understanding Android Intents

An Intent in Android is a software mechanism for describing a desired action, such as 'view this photo', or 'open this webpage'. Intents are used to navigate between Activities and to pass data between them.

Intents can be explicit, where you specify the target Activity, or implicit, where you describe the desired action and Android figures out the target Activity.

Code Examples

1. Creating an Activity

First, create a new Activity Java file, MainActivity.java:

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }
}

In this code:
- MainActivity extends the Activity class, which means it inherits all the functionality of an Activity.
- onCreate() is a method that gets called when the Activity is created. It's where you initialize your Activity.
- setContentView(R.layout.activity_main); sets the layout for this Activity to activity_main.xml.

Next, create the corresponding layout XML file, activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello, World!" />
</RelativeLayout>

Here, we've defined a simple layout with a TextView that displays "Hello, World!".

2. Creating an Intent

Let's say we have another Activity, SecondActivity.java, and we want to navigate to it from MainActivity.java. Here's how:

import android.content.Intent;
import android.view.View;

public class MainActivity extends Activity {
  public void goToSecondActivity(View view) {
    Intent intent = new Intent(this, SecondActivity.class);
    startActivity(intent);
  }
}

In this code:
- We create an Intent with new Intent(this, SecondActivity.class). The first parameter is the source Activity (this), and the second parameter is the target Activity (SecondActivity.class).
- We start the target Activity with startActivity(intent).

Summary

In this tutorial, you've learned about Android Activities and Intents. You've learned how Activities represent the screens of an Android app, and how Intents allow for navigation and data exchange between Activities. You've also learned how to create Activities and Intents in your Android app.

Next steps:
- Learn about Activity lifecycle and states
- Learn about different types of Intents

Additional resources:
- Android Developer's Guide
- Android Basics in Kotlin

Practice Exercises

1. Create an Application with Multiple Activities

Create an Android application with at least three Activities. Use explicit Intents to navigate between these Activities.

2. Passing Data Between Activities

Modify your application from the previous exercise to pass data between Activities. Use Intents to pass data.

Solutions and explanations for these exercises can be found in the Android Developer's Guide. For further practice, try to create an application that uses implicit Intents to open a webpage or call a phone number.