How To Transfer Data Between Activities Using Intent

Hello friends, today I will tell you how to transfer data between activities using intent. In the previous post I wrote How To Make A Simple Calculator App In Android.

Now the first thing comes in your mind is that what is an activity. All the thing that are available on the screen with which you can interact with is called an activity. It is just like Applets in Java, but in Android, we have an activity. The user interface is drawn on the activity so that the user can be able to perform different tasks.

In a single android application, we can have multiple activities, depending upon the functionality of the application.

The interaction between different activities can be done by using Intents. I will explain this with the help of an example.

Like there are 2 activities within same application namely Activity A,  Activity B. Now the work of Intent is to call the Activity B and display it in front of user.

Basically there are two types of Intents:

  1. Explicit Intents
  2. Implicit Intents.

Explicit Intents are the intents that are used when we know in advance to which activity we have to call so as to get it displayed in front of user. The above-explained example is a type of Explicit Intent in which we know that we have to call Activity B.

Implicit Intents are the intents that are used when we don’t know who to call. For Example: you have to make a call or to send an sms or an email and you know the functionality how to do that but you don’t know to which particular activity you want to call to do this, that’s where we use Implicit Intents. The android will do this for you. It chooses the right activity for you.

Now, to start the new activity, we use startActivity() method. This method will take the object of the Intent that we will make when we call that class which we want to open using Intent.

Intent intent= new Intent(Activity_A.this, Activity_B.class);
startActivity(intent);
  • Within Intent we pass two things, first is the Context and the other is the name of activity you want to start. The context here is that set the environment for your android application by which you can access all the resources. In the above code, the Activity_A.this is the context.
  • The second one is the name of the activity you want to display to the user.

Now many a times you need to transfer data between activities. This will be done using putExtra() and  getExtra() methods. The putExtra() method will send the data to another activity and the getExtra() method takes the data using getintent().

Intent intent= new Intent(Activity_A.this, Activity_B.class);
i.putExtra("key","Aditya");
startActivity(i);
  • To send data to other activity we use method putExtra(). We have to pass two things in this, first is  a key and second is the string that we have to send to second activity.
  • The key is the value used by the second activity so as to get the data that is send by the first activity.
Intent intent=getIntent();
String name=intent.getString("key");
  • Now the key is that value which was passed in intent along with the string.
  • And the name is now containing the string that was passed with intent in Activity_A.class.

We can show that string within the text view and set that text view so that it can be shown to the user.

TextView textview = (TextView) findViewById(R.id.textview);
textview.setText("My name is:"+name);

After all the things are done, you have to add the Activity_A and Activity_B in the manifest file.

Transfer  Data Between Activities Using Intent

Now we have discussed all the important points that are used to transfer data between activities using intent, so we will use do a practical example.

  • In this we will take two activities, the first one will be the Activity_A and the second will be Activity_B.
  • Then we have to create two xml file named as activity_a.xml and activity_b.xml.

So let’s start.

  • Firstly create a project in Android Studio.
  • Write the name as TransferringDataUsingIntent and click next.
  • After that select a blank activity and click next.
  • Then, change the name of MainActivity as Activity_A and click Finish.
  • Now create another blank activity and name it as Activity_B.class.

In activity_a.xml do following changes:

<?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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.aditya.transferringdatausingintent.Activity_A"
    tools:showIn="@layout/activity_a">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Name"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="86dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Email"
        android:id="@+id/textView2"
        android:layout_below="@+id/textView"
        android:layout_alignParentStart="true"
        android:layout_marginTop="45dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:hint="Your Name"
        android:ems="10"
        android:id="@+id/editText"
        android:layout_alignBottom="@+id/textView"
        android:layout_alignParentEnd="true"
        android:layout_toEndOf="@+id/textView" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:ems="10"
        android:hint="Your Email"
        android:id="@+id/editText2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignParentEnd="true"
        android:layout_toEndOf="@+id/textView" />
</RelativeLayout>

This will be the first activity that the user will see when the application starts. This above code is the layout for our first activity.

Now go to Activity_A.class where we have to do our coding and do following changes:

package com.example.aditya.transferringdatausingintent;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Activity_A extends AppCompatActivity implements View.OnClickListener {

    TextView textView1,textView2;
    EditText editText1,editText2;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        textView1=(TextView) findViewById(R.id.textView);
        textView2=(TextView) findViewById(R.id.textView2);

        editText1=(EditText) findViewById(R.id.editText);
        editText2=(EditText) findViewById(R.id.editText2);

        button=(Button) findViewById(R.id.button);
    }

    @Override
    public void onClick(View v) {
        
        String name=editText1.getText().toString();
        String emai=editText2.getText().toString();

        Intent intent=new Intent(Activity_A.this,Activity_B.class);

        intent.putExtra("name",name);
        
        intent.putExtra("email",emai);
        
        startActivity(intent);


    }
}

In the above code we have take two edit texts whose value will be passed to the Activity_B. I will explain the code one by one

  • Firstly we have declared two edit texts, two text views and a button.
  • Then in onCreate() method, we have find the id’s of the edit text, text view and button using findViewById method.
  • After that the onClick() method is created. In the method we write the coding of what will happen if the user clicks on the button in simple words this it acts as OnClickListner() for a button.
  • In this we have taken two strings, name and email, which will store the string that the user enters in the edit text.
  • Then we used Intent method and passed the context and the Activity_B.class as I explained above.
  • Now within intent.putExtra() I have passed 2 things, the key and string that we have to send to the second activity.

Now within activity_b.xml do the following changes:

<?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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.aditya.transferringdatausingintent.Activity_B"
    tools:showIn="@layout/activity_b">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/textView3"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="64dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_marginTop="40dp"
        android:id="@+id/textView4"
        android:layout_below="@+id/textView3"
        android:layout_alignParentStart="true" />
</RelativeLayout>

In the above code we have set the layout for Activity_B.

Now open Activity_B.class and do following changes in it:

package com.example.aditya.transferringdatausingintent;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

public class Activity_B extends AppCompatActivity {

    TextView textView3,textView4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_b);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        textView3=(TextView) findViewById(R.id.textView3);
        textView4=(TextView) findViewById(R.id.textView4);


        Intent intent=getIntent();

       String name = intent.getStringExtra("name");
     String email = intent.getStringExtra("email");

        textView3.setText("My Name is : " + name);

        textView4.setText("My Email is :" +"\n" +email);

           }

}
  • In the above code, firstly the text views are declared and in onCreate method, the id’s of these text views is founded.
  • Then the Intent used to get the data from Activity_A.
  • After that, the string passed from Activity_A is extracted from the getIntent() method using the key that was used when the data was send to Activity_B  from Activity_A.
  • Then we have set the data in their respective text views.

Now our code is ready and we can run our application.

Activity_A

Activity_A

 

Activity_B

Activity_B

 

Congratulations, we have successfully created out app on transferring data between activities using intent. If you are a begineer in android then, then firstly understand What is Android and Architecture of Android.

One Response

  1. Kamal Thakur April 27, 2016