Passing data between activities in Android with Example

Posted By on March 23, 2016


Download PDF
Start New Activity Example
Android Fundamentals and Components

Using 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. MainAcivity.java (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).

Start New Activity Example
Android Fundamentals and Components

Download PDF

Posted by Akash Kurup

Founder and C.E.O, World4Engineers Educationist and Entrepreneur by passion. Orator and blogger by hobby

Website: http://world4engineers.com