In this tutorial we will learn 2 basic things.Pass data between activities and add a back arrow. There are multiple ways to pass data between activities in android studio. Today we will use the easiest way to pass data from one activity to another. We will also add add a back arrow in Android studio. So we simply need 2 activities for this example and 3 to 4 lines of code to send and receive data.
So let’s get started.
What we will learn in this tutorial and How to:
- Add a Back arrow in app-bar
- Create New Activity in Android Studio
- Call Another Activity in Android Studio
- Pass data between two activities in Android Studio
- Only enable digits to be entered in Edit Text field
- Call Button’s onClick() Listener in Android Studio
- and much more…
Steps to pass data between activities
- Create or Open Android Studio Project
- Add EditText fields in 1st activity to get user data
- Create another receiving Activity
- Pass data from 1st activity onClick() of button using Intent
- Receive data using Intent in 2nd activity and show on screen
Create or open Android Studio Project

Design The XML of First Activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:padding="20dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/nameET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name" />
<EditText
android:id="@+id/numberET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/nameET"
android:layout_marginTop="10dp"
android:hint="Name"
android:inputType="number" />
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/numberET"
android:layout_marginTop="30dp"
android:text="Send Data" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="First Activity!" />
</RelativeLayout>
Code MainActivity.Java to send data
public class MainActivity extends AppCompatActivity {
EditText name, number;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Hooks
name = findViewById(R.id.nameET);
number = findViewById(R.id.numberET);
btn = findViewById(R.id.btn);
//Pass Data on Button Click
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Get data from input field
String getName = name.getText().toString();
String getNumber = number.getText().toString();
//Pass data to 2nd activity
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("name", getName);
intent.putExtra("number", getNumber);
startActivity(intent);
}
});
}
}
Design the 2nd XML layout to show received data
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:padding="20dp"
tools:context=".SecondActivity">
<TextView
android:id="@+id/set_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textSize="24sp" />
<TextView
android:id="@+id/set_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/set_name"
android:layout_marginTop="10dp"
android:text="123"
android:textSize="24sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Second Activity"
android:textSize="24sp" />
</RelativeLayout>
Code in the 2nd Java to get data
public class SecondActivity extends AppCompatActivity {
TextView name, number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
//Hooks
name = findViewById(R.id.set_name);
number = findViewById(R.id.set_number);
//Get text from Intent
Intent intent = getIntent();
String getName = intent.getStringExtra("name");
String getNumber = intent.getStringExtra("number");
//Set Text
name.setText(getName);
number.setText(getNumber);
}
}
Related Articles that you might want to check out.
- How to create a Splash Screen with Animations
- Create a new project in android studio for beginners?
- How to create an Account on Fiverr and start earning?
- Create a Gig ob Fiverr and start selling specific services.
- How To Start Blogging and Make Money?
Our Projects Make Us Proud
- Building and Construction Company
- Chakwal.CO | Group Of Companies
- Online Buying and Selling Of Properties
My Latest Work