How to Send Data From One Activity to Another in Android

In this post you will learn how to send data from one activity to another in android,

we will use intent to pass data from one activity to another in android.

Intents are very useful in term of communicating between activities

or between application and OS.

 

So let’s start our tutorial by creating new project in android studio

 

Basically in this tutorial we will get text from our edittext on button click and then we

will set that text to our textview, after setting the text we will pass the textview value to another

activity in our android application.

 

So first thing first create a new empty activity in your project name it ‘SecondActivity’ like below

 

send data from one activity to another in android

 

Now open your activity_main.xml and paste the following code

 

 

We need to initialize all these views in our java file, to do so open your MainActivity.java file and paste the following code:

 

 

In the above code we are getting text value from our edittext and setting it to our textview and then on the button

click we are getting text value from our textview and passing it to another activity using intent extras.

Now we will handle our intent extras in our newly created SecondActivity.

 

Open activity_second.xml file and paste the following code

 

 

In our SecondActivity.java we will handle our intent extras coming from MainActivity.java, so open your SecondActivity.java 

and paste the following code

 

 

The above is using Bundle class to get intent extras from MainActivity and then we are setting the intent extra value to our textview.

That’s it our application is ready to send data from one activity to another in android, just run your application and pass some data

if you have any question then comment below I will answer it.

Written by Hilal Ahmad

Leave a Reply

Your email address will not be published. Required fields are marked *