DEV Community

Cover image for How to switch activity and pass data using extras.
Rishabh Kumar
Rishabh Kumar

Posted on • Originally published at codewithrish.com

How to switch activity and pass data using extras.

Create Android Studio Project.

Select empty activity while creating the project. one project is ready you will have an empty MainActivity

Create Another Activity

Name your newly created activity as HomeActivity. we have two methods to start new activity one is startActivity() and other is startActivityForResult() each have their own use-cases.

Snippet to use startActivity() method

// create intent object with context and class name of other activity
Intent intent = new Intent(this, HomeActivity.class);
// call startActivity method and pass intent
startActivity(intent);
Enter fullscreen mode Exit fullscreen mode

Passing data

putExtra() method of intent required for passing data

snippet

// put data in intent
intent.putExtra("username", "codewithrish"); // you can pass any primary data type easily 
Enter fullscreen mode Exit fullscreen mode

Receiving data in HomeActivity

getStringExtra() method helps us to receive string simlirarily getIntExtra() for receiving integer value so on.

Complete Code Step by Step

Open MainActivity.java paste code

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

public class MainActivity.java extends AppCompatActivity {

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

        Button button = findViewById(R.id.btn_open_activity);
        button.setOnClickListener(click -> {
            openHomeActivity();
        });
    }

    private void openHomeActivity() {
        // create intent object with context and class name of other activity
        Intent intent = new Intent(this, HomeActivity.class);

        // put data in intent
        intent.putExtra("username", "codewithrish");

        // call startActivity method and pass intent
        startActivity(intent);
    }

}
Enter fullscreen mode Exit fullscreen mode

Open activity_main.xml paste code

<?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=".MainActivity">

    <Button
        android:id="@+id/btn_open_activity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go To Home Activity"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
Enter fullscreen mode Exit fullscreen mode

Open HomeActivity.java paste code

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class HomeActivity extends AppCompatActivity {

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

        // get intent
        Intent intent = getIntent();
        // retrieve username from intent
        String username = intent.getStringExtra("username");

        // find text with id
        TextView tvUsername = findViewById(R.id.txt_username);
        //set username to that textview
        tvUsername.setText(username);
    }
}
Enter fullscreen mode Exit fullscreen mode

Open HomeActivity.java paste code

<?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=".HomeActivity">

    <TextView
        android:id="@+id/txt_username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Enter fullscreen mode Exit fullscreen mode

Run code

Just Run Code you just learn how to switch activity and pass data

Top comments (0)