Passing data between activities in Android – Example

android-logoBy taking a simple example, we will learn the simplest way about how to passing data between activities in Android. 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.


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

Deacriptions – Passing data between activities Example

Here in the below example, we will take 2 simple activities. We will send different data with data-types like String, Integer, Long etc. Then we will receive the same value from the second activity. From the first activity we will put data ( In our example: we have taken Name: a string data type , Phone number : long datatype), then from the second activity we will retrieve the name and phone number and show using a TextView.

Note: Download the code  and have a look into line by line comments which are written in the source code.

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 (54 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 – 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 *

eight − = 0

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="">