---
title: "How to pass data one activity to another activity?"  
description: "How to pass data one activity to another activity?"  
author: "Pawan Shukla"  
published: 2018-02-27  
canonical: https://answers.mindstick.com/qa/35040/how-to-pass-data-one-activity-to-another-activity  
category: "android"  
tags: ["android", "activity"]  
reading_time: 4 minutes  

---

# How to pass data one activity to another activity?

How to [pass data](https://www.mindstick.com/forum/23323/how-to-pass-data-from-action-to-ajax-success-function-in-mvc4) one [activity](https://www.mindstick.com/blog/23160/fidgetsguide-exercise-guides-newsletters) to another activity?

## Answers

### Answer by Arti Mishra

**Pass [data](https://yourviews.mindstick.com/view/84027/what-does-google-do-with-your-data) from one activity to another activity in android:** There are two ways to send data from one activity to another activity in android. 1. By Using Intent 2. By using Global Variable Here we are using Intent, It is also used to send data from one activity to another activity using putExtra() function. The data is passed in key value pair form. The value can be any types like int, float, long, string, etc. \
**Sending Data**

```
Intent intent=new Intent(context,AnotherClassName);
intent.putExtra(key, value);
startActivity(intent);
```

**Receiving Data** If the data is sent string type then it can be receive in following ways.

```
Intent intent=getIntent();
String str=intent.getStringExra(Key);
```

\
There are several other methods like getIntExtra() for int, getFloatExtra() for float, etc to fetch other types of data. **Example** Below is the example, a name is passed from first activity to second when a button is pressed.\
**activity_first_main.xml**

```
<?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"
    tools:context="com.example.msclient009.opennewactivity.FirstMainActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/editText"
        android:layout_marginTop="49dp"
        android:text="This is First Activity"
        android:textColor="#f00"
        android:textSize="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="79dp"
        android:hint="Enter Your Name...." />
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/editText"
        android:layout_marginLeft="25dp"
        android:layout_marginStart="25dp"
        android:layout_marginTop="63dp"
        android:text="Click Here to open new Activity and Send Your Name" />
</RelativeLayout>
```

\
**FirstMainActivity.java**

```
package com.example.msclient009.opennewactivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class FirstMainActivity extends AppCompatActivity {
    EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_main);
        Button button=(Button)findViewById(R.id.button1);
         editText=(EditText)findViewById(R.id.editText);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str=editText.getText().toString();
                Intent intent=new Intent(FirstMainActivity.this, NewMainActivity.class);
                intent.putExtra("Name", str);
                startActivity(intent);
            }
        });
    }
}
```

\
**activity_second_main.xml**

```
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="110dp"
        android:text="This is Second Activity"
        android:textColor="#00f"
        android:textSize="30dp" />
    <TextView
        android:id="@+id/textView2"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/textView1"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginEnd="46dp"
        android:layout_marginRight="46dp"
        android:textColor="#fff"
        android:layout_marginTop="60dp"
        android:background="#f00"
        android:text="Arti mishra"
        android:textSize="30dp" />
</RelativeLayout>
```

\
**FirstMainActivity.java**

```
package com.example.msclient009.opennewactivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class FirstMainActivity extends Activity {
 TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second_main);
        textView=(TextView)findViewById(R.id.textView2);
        Intent intent=getIntent();
        String str = intent.getStringExtra("Name");
        textView.setText(str);
    }
}
```

\
**output :****\**![How to pass data one activity to another activity?](https://answers.mindstick.com/questionanswer/4b858422-6301-48ec-9570-194975de55e6/images/9da96dde-93d9-48b7-945a-bea398e43809.png)**\****\** **After Clicking on the button :****\**![How to pass data one activity to another activity?](https://answers.mindstick.com/questionanswer/4b858422-6301-48ec-9570-194975de55e6/images/55d4dd43-c8a5-41ef-b3c5-52b2b26f85fb.png)\
\


---

Original Source: https://answers.mindstick.com/qa/35040/how-to-pass-data-one-activity-to-another-activity

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
