Launch Activity

An android app user interface is made up of activities and fragments. At some point you may need to navigate from one activity to another. In this tutorial we will discuss how to navigate from one activity to another with a data passed to next activity. Also to return with a data to previous activity. If this doesnt make any sense, Follow along the tutorial and you will understand.

What we are going to do

  • Create two activities

  • send data from first activity to second activity

  • return data from second activity to first activity

Lets start by creating new project

Im simply creating a project with name "ActivityTransition". In the project creation dialog, choose empty activity and rename MainActivity to FirstActivity and make sure its selected as launcher activity. Then click finish and wait for it finish build. Now We need to create second activity and name it SecondActivity.

To get a simple user interface as below simply use the code below.If you prefer typing simply type it.

activity_first.xml
<?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=".FirstActivity">

    <EditText
        android:id="@+id/editText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="48dp"
        android:layout_marginEnd="16dp"
        android:ems="10"
        android:hint="Username"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:text="Second"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText" />
</androidx.constraintlayout.widget.ConstraintLayout>

activity_second.xml
<?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=".SecondActivity">

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="16dp"
        android:text="First"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Now If you simply want to navigate to second activity with an extra data simply copy the below code to FirstActivity.java

FirstActivity.java
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class FirstActivity extends AppCompatActivity {

    private Button button;
    private EditText editText;

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

        button = findViewById(R.id.button);
        editText = findViewById(R.id.editText);
        
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
                intent.putExtra("username", editText.getText().toString());
                startActivity(intent);
            }
        });
    }
}

Paste the code below inside onCreate() method in the second activity to get the passed data and show it as toast.

SecondActivity.java
String username = getIntent().getStringExtra("username");
Toast.makeText(this, username, Toast.LENGTH_SHORT).show();

Or if you want to retreive result in first activity that is to send data from second activity back to first activity, use startActivityForResult method instead of startActivity and implement onActivityResultMethod. you can implement by pressing ctrl + O and search onactivityresult or simply pase below code. Pass a intent code so that you can use in onActivityResultMethod that is implemented in First Activity.Use below code instead of startActivity

FirstActivity.java
startActivityForResult(intent,100);

FirstActivity.java
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 100 && resultCode == RESULT_OK){
        if (data != null){
            Toast.makeText(this, data.getStringExtra("email"), Toast.LENGTH_SHORT).show();
        }

    }
}

And in SecondActivity add this code inside onCreate to navigate back to firstactivity with a result data. We are simply passing an email

SecondActivity.java
button = findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent = new Intent(SecondActivity.this,FirstActivity.class);
        intent.putExtra("email","example@mail");
        setResult(RESULT_OK,intent);
        finish();
    }
});

Now if you run you should get something like this. All source code is found here

Last updated