Photo by Suika Ibuki on Unsplash

Passing data between Activities using Intent in Android

Bhavya Varmora
AndroidPub
Published in
5 min readJul 3, 2019

--

In this tutorial, we will learn how to use Intent and then we will pass the data using Intent from one activity to another.

What is Intent??

Through Intent we can move from one activity to another activity and Intent can also be used to pass the data from one activity to another activity.

In the previous article, we designed the Login Page and now we will learn how to use Intent to pass data from LoginActivity to the next activity.

Step 1:

First of all, we have to link the views of activity_login.xml with LoginActivity In activity_login.xml, we have used two EditTexts and one button. Lets initialize them in our LoginActivity.

public class LoginActivity extends AppCompatActivity {

EditText edittextfullname,edittextusername;
Button loginbutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);

edittextfullname=findViewById(R.id.editTextFullName);
edittextusername=findViewById(R.id.editTextUserName);
loginbutton=findViewById(R.id.LoginButton);
}
}

EditText edittextfullname,edittextusername; : This is the way to define the views which we have used in the layout and you can give any name you want. Here we have given edittextfullname and edittextusername.

Button loginbutton; : Here we have defined a button with name loginbutton.

Now lets link the views using findViewById in LoginActivity.

edittextfullname=findViewById(R.id.editTextFullName); : This line help to find the id by findViewById method and the id which we have given for widget in layout XML file.

edittextusername=findViewById(R.id.editTextUserName); : This line does the same thing as edittextfullname does.

loginbutton=findViewById(R.id.LoginButton);:This line does the same thing as edittextfullname and edittextusername does.

We have successfully linked the views defined in the activity_login with LoginActivity.

Step 2 :

Whats Next ???

Now we have to create a new activity so let’s create an activity with the same procedure we have gone through in the previous article.

Name the new activity as SecondActivity.

Now you have created two Activities. With the help of Intent,we will go from one activity to another activity (LoginActivity to SecondActivity).
Now its time to use Intent but first we have to learn setOnClickListener.

Step 3:

When we press the button, setOnClickListener() will be called and inside setOnClickListener(), we will write code to open new activity and we can do this with the help of Intent.

So, let’s code for better understanding.

loginbutton.setOnClickListener(){}

So in the parentheses write “new View.OnClickListener” and click View.OnClickListener from the pop-up.

It will look like the image below.

Step 4 :

If we have to open one activity from another activity we have to add an Intent code in onClick(View view) method as shown below.

How to use Intent method?

This is the method to use Intent.

Intent intent = new Intent(Source, Destination);
startActivity(intent);

Source: It means the current activity in which you are present.

Destination: It means the activity where you have to go.

startActivity(intent); : This starts the activity.

The below code adds into the onClick method.

Intent intent = new Intent(LoginActivity.this,SecondActivity.class);
startActivity(intent);

When you press Login Button it will successfully open the SecondActivity.

Step 5:

How we pass data through Intent?

To pass the data through Intent we will use putExtra() method and in parameter, we will use Key-Value Pair.

Now, where we have to mention putExtra() method?

We have to add putExtra() method in onClick() as shown in the below code and in parameter we have to mention key and its value. So here we have to pass the data of fullName and hence we have given a unique key as full_name and value as fullName.

Where does the data come from?

It comes from edittextfullname when the user enters any name in EditTexts it is stored in edittextfullname using the below code.

String fullName = edittextfullname.getText().toString();

As you can see that we have made a variable of fullName and it's of String data type.

edittextfullname.getText().toString() : This means when the user enters the name in edittextfullname we get the entered value from this method with the help of getText() and then the value or name is converted in to string by using toString().

putExtra() : This method sends the data to another activity and in parameter, we have to pass key-value pair. Add the below code in onClick() method.

intent.putExtra("full_name", fullName);

So, the onClick() will be as shown below:

Step 6:

Now open the SecondActivity.

Our next task is to print fullName in ActionBar of SecondActivity. Have a look at the below code.

I will explain the code written in the above picture.

String fullname : We have declared a variable of String type.

Intent intent=getIntent() : It gets the Intent from the LoginActivity.

fullname= intent.getStringExtra(“full_name”) : This line gets the string form LoginActivity and in parameter, we have to pass the key which we have mentioned in LoginActivity.

getSupportActionBar().setTitle(fullname): This line sets the fullname in ActionBar as a Title.

Run the project and it should work as expected.

This is the Picture of SecondActivity

The data is successfully passed as you can see. The name written in FullName is set on on ActionBar as Title.

So in my next article, we will learn how to add validations in the LoginActivity.

Feel free to comment if you have any doubts or suggestions.

Also, let’s become friends on Twitter and Instagram.

Follow me on Youtube https://www.youtube.com/results?search_query=bhavyavarmoravlogs

Thank You.

--

--