Java / Java Android Development

Understanding Android Activities and Intents

In this tutorial, you will learn about two fundamental concepts in Android development: Activities and Intents. Activities are the screens of an Android app, while Intents allow f…

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Explores Android development using Java for building mobile apps.

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.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Base64 Encoder/Decoder

Encode and decode Base64 strings.

Use tool

Random Name Generator

Generate realistic names with customizable options.

Use tool

Text Diff Checker

Compare two pieces of text to find differences.

Use tool

AES Encryption/Decryption

Encrypt and decrypt text using AES encryption.

Use tool

Fake User Profile Generator

Generate fake user profiles with names, emails, and more.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help