Passing data between activities in Android with Example

android-logoUsing a simple example, We will learn  about passing data between activities in Android with example. It is very easy to start a new activity from another activity with some data (like string , integers etc…). Here we will take a very simple example, which will give us some simple and clear idea about how to sending data form one activity to another activity. Note: You can download complete source code from below and have a look into in line comments which are written in side the source code.

passing data between activities in android example  passing data between activities in android example

Descriptions – Passing data between activities using simple Android Intent

In the below example, We will take 2 simple activities. Then we will send different types of data (data-types like String, Integer, Long etc). Then we will receive the same values from the second activity. From the first activity we will put some data ( In our example: we have taken a Name: a string data type, Phone number : Long datatype), then from the second activity we will retrieve the same data (name and phone number) and we will show these data using a TextView on an Activity’s screen.

1. activity_main.xml (GUI component  for the first activity)

This xml file is used in the first activity (MainActivity). This xml file holds a button(which will use for start a new activity with some data) and 2 EditText field (which are input fields fro name and number).

 2. (First Activity).

In this activity, we will get user input for Name and Number and then start a new activity with those values. In the below code, intent.putExtra() is the function which will do the actual job for us. Have a look into onStartNewActiviity() function below.

 3. activity_second.xml (GUI component  for the second activity)

In this xml file, we just used a TextView to show the name and phone number data, which came from the first activity.

 4. SecondActivity,Java (Retrieving data and showing it on screen)

Here we will retrieve name and phone number, which has been sent by first activity, then we will show it on the screen using TextView.  In the below code getIntent().getStringExtra()  is the function which do the job for retrieving data for us. Have a look into the below code.

 5. AndroidManifest.xml (Android Manifest file)

Don’t forget to add your activities name in the manifest file (in our case: MainActivity and SecondActivity). Have a look into the Manifest file for our example.

 Note: To make this example simple, null point checks are not added (like Name and Phone number field in the code).

downloadDownload complete source code for Passing data between activities in Android – Example from Here.

I hope this small tutorial on how to pass data between activities in Android will help you at it’s best.

Post By SmrutiRanjan (57 Posts)

Working @ Samsung as a Project Lead for Android Smartphones. I have been blogging since 2008. Previously I was writing articles for other bloggers, but finally I have started my own blog-"Techblogon".I am also an active contributor for the blog-"Gadgets n Gizmos World". Job is my necessity, but blogging is my passion.

Website: → Techblogon


2 Responses to Passing data between activities in Android with Example

  1. sathish kumar says:

    hi SmrutiRanjan,

    It is useful to me and very very thanks to you..

  2. Satish Singh says:

    thanks sir

Leave a Reply

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

three + 8 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">