Beginner 10 min Java Full project

Open a New Activity in Android Studio (Java)

Another quick walkthrough of opening a new activity with an Intent, step by step.

What you'll learn

  • Create a second activity
  • Open it from a button with an Intent
  • Register it in the manifest

Before you start

  • Android Studio (any recent version)
  • An emulator or an Android phone

How it fits together

  1. 1MainActivity
  2. 2Intent with extras
  3. 3SecondActivity
  4. 4Result back to MainActivity

Build it step by step

1. Create the project

In Android Studio choose New Project → Empty Views Activity, with Language Java and Minimum SDK API 24.

2. Add a second screen

Right-click your package → New → Activity → Empty Views Activity, named SecondActivity. Android Studio registers it in the manifest.

3. Open it with an Intent

An Intent says "open this screen". new Intent(this, SecondActivity.class), then start it.

4. Send data along

intent.putExtra("name", ...) attaches the name. In SecondActivity, getIntent().getStringExtra("name") reads it back.

5. Go back

finish() closes the second screen and returns to the first, just like the Back button.

6. Send a reply back

Start the screen with registerForActivityResult. In SecondActivity, call setResult(RESULT_OK, intent) before finish(). The first screen receives the reply in its callback.

7. Run it

Type your name, open the second screen, then tap Wave back.

Tip

Use extras for small values like names and IDs. For bigger data, save it (for example in a database) and pass just the ID.

What's improved over the video

It passes data between screens and back again, using the current registerForActivityResult API.

Application code

Select a file to see its code and where it belongs.

This code uses package com.example.switchactivities. Change that first line to your own package, the one at the top of your MainActivity.

Replace fileapp/src/main/java/com/example/switchactivities/MainActivity.java

Opens SecondActivity with the name, and receives its reply.

MainActivity.java
package com.example.switchactivities;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.textfield.TextInputEditText;

/**
 * Opens SecondActivity with an Intent, passing the name along, and gets a reply back when it closes.
 */
public class MainActivity extends AppCompatActivity {
    TextView reply;

    // Starts SecondActivity and receives its reply when the user comes back.
    private final ActivityResultLauncher<Intent> openSecond = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(), result -> {
                if (result.getResultCode() == RESULT_OK && result.getData() != null) {
                    reply.setText(getString(R.string.reply, result.getData().getStringExtra(SecondActivity.EXTRA_REPLY)));
                }
            });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Screens.setUp(this, R.layout.activity_main);

        TextInputEditText name = findViewById(R.id.name);
        reply = findViewById(R.id.textReply);
        Button open = findViewById(R.id.btnOpen);

        open.setOnClickListener(v -> {
            Intent intent = new Intent(this, SecondActivity.class);
            // Extras carry small pieces of data to the next screen.
            intent.putExtra(SecondActivity.EXTRA_NAME, String.valueOf(name.getText()).trim());
            openSecond.launch(intent);
        });
    }
}

Full source code