Android Tutorial – Transfer Data Between Activities Using Intent

In previous android development tutorial I wrote about Volley Library in details and how can we import Volley library in our project. I also explained how we can parse JSON data using volley library.

Android developers can find all my android tutorials in Android section.

In Android, user interface is displayed through an activity(Screen). Activity is used to represent the data to user and allows user interaction. In an android application, we can have multiple activities and they can interact with each other.

Interaction between multiple activities can be done only using Intent. Intents are objects of the android.content.Intent type. Your code can send them to the Android system defining the components you are targeting. In Android app development you face situations where you want to send or receive your data between one Activity (Screen) to another. In this android tutorial I will be discussing about how to Send or Receive data from one Activity to another.

Before getting started with sample app development, I will explain what are the methods needed to send data between activities using Intent.

  • Lets assume that our first activity is FirstScreen.Java and second activity is SecondScreen.Java.
  • To send data from one activity to other we have to use Intent.
  • An Intent is exactly what it describes. It’s an “intention” to do an action. An Intent is basically a message to say you did or want something to happen.
  • Intents are objects of the android.content.Intent type. Your code can send them to the Android system defining the components you are targeting. For example, using startActivity() method you can define that the intent should be used to start an activity.
  • To start or open a new activity we use startActivity(intent) or startActivityForResult(intent,Request code) methods will be used.
  • startActivity() method is used when you just wanted to start an activity.

  • startActivityForResult(intent, requestCode) when you expecting some back data from other activity then, this method is useful.

  • To send data from one Activity to another activity, putExtra(String, Any primitive value) method will be used.
  • putExtra() method contain parameter as key and value. Here String is a key and primitive is a value.

  • To recieve data from an activity, we can use either Intent.getStringExtra(key) or Bundle.getString(key).

  • Bundle is a class similar to Map which stores primitive datatype and Objects as Key-Value pair.
  • Bundle is most often used for passing data through various activities.

  • To read the result from the closed Activity, we use onActivityResult(int requestCode, int resultCode, Intent data).

  • To close an activity we use finish() method.
  • If we create a new Activity then we must have to add this activity to AndroidManifest.xml file.
  • AndroidManifest.xml file is like the configuration file for our application.
  • We add new Activity between <Application> tag.

Android Development Tutorial : Sending Data Between Activities Using Intent

Now we know all the important methods that are used in our project. For this android tutorial for android developers I created two xml layout files which are first_screen.xml  and second_screen.xml. There is also two java files which are FirstScreen.Java and SecondScreen.Java file. Lets get started.

Assumpt ions

So lets gets started with our sample android App:

  • Open your Android Studio.
  • Click on Start a New Android Studio Project.
  • Write your Application Name “DataTransferBetweenActivities and leave other fields blank as it is, then click NEXT.
  • Select the Minimum SDK API 15: Android 4.0.3(IceCreamSandwich)”I selected API 15 because it covers 94% device and it has almost all the features. If you want to cover 100% device then you can select API 8: Android 2.2(Froyo). But it has limitation on features. After selecting your API click on NEXT.
  • Select the blank activity and click NEXT.
  • Write the activity name “FirstScreen” and leave everything as it is. Click Finish.
  • After clicking Finish, it takes around 5 minutes to build your basic hello world app.
  • Now we design our first_screen.XML file.
  • Go to app -> res -> layout -> first_screen.XML.
TypeIDVALUEXML File
TextViewtextView1First Screenfirst_screen.xml
TextViewtextView2first_screen.xml
EditTexteditText1 first_screen.xml
EditTexteditText2 first_screen.xml
EditTexteditText3 first_screen.xml
Buttonbutton1 Click Me first_screen.xml

Below is the code for first_screen.XML file

Android Development Tutorial : Data Transfer Between Activities Using Intent Object

Android Development Tutorial : Data Transfer Between Activities Using Intent Object

  • Now create a XML file for Second Screen. Go to app -> res -> layout -> Right click -> New -> XML -> layout XML file.
  • Give layout file name as second_screen.
TypeIDVALUEXML File
TextViewtextView3Second Screensecond_screen.xml
 TextViewtextView4Full Namesecond_screen.xml
TextViewtextView5 second_screen.xml
TextViewtextView6Date Of Birthsecond_screen.xml
TextViewtextView7 second_screen.xml
EditTexteditText4 second_screen.xml
Buttonbutton2Confirm second_screen.xml

Below is the code for second_screen.XML file

Android Development Tutorial : Data Transfer Between Activities Using Intent Object Android App

second_screen.XML

  • Now Go to the java file in which we will do coding for the action performed by app.
  • Go to app -> Java -> com.abhinavhackpundit.DataTransferBetweenActivities -> FirstScreen.Java .

Below is the code for FirstScreen.Java file

Below is the explanation of highlighted lines because that Java code  is behind First Screen.

  • Line 16-18: Here we have taken the Reference of TextView, EditText and Button.
  • Line 26-31: Here we have done TypeCasting of each into TextView, EditText and Button.
  • As findViewById returns View, which is needs to be Typecast into respective Element.
  • Line 33: Here we setting the Click Listener on btn1.
  • Line 36: To send data from one Activity to another activity we have to use Intent. So here we created the reference of Intent as intent1. 
  • Intent(FirstScreen.this, SecondScreen.class), here in the intent method we pass the Source and Destination as parameter.
  • Line 38-40:  We send data using PutExtra() method. the first parameter must be String  and second parameter is any primitive value in putExtra() method.
  • Line 41-43: If you simply send data and not expecting ant data in return then use startActivity(intent1). else call startActivityForResult(intent,1).
  • Line 49 -52: If you expecting some data from other activity then to read data we have to use onActivityResult() method.
  • Line 54 – 82: Here i called all the built in method and call the method mt() which is define in Line 90-92 .
  • This helps you to understand the lifecycle of Activity.

Now we have to create a JAVA file in which we will do coding for the action performed by SecondScreen.

  • Go to app -> Java -> New -> Java class. Name it as SecondScreen.Java.
  • Go to app -> Java -> com.abhinavhackpundit.DataTransferBetweenActivities -> SecondScreen.Java .

Below is the code for SecondScreen.Java

Below is the explanation of highlighted lines because that Java code  is behind Second Screen.

  • Line 17-19: Here we created the reference of TextView , EditText and Button.
  • Line 25: Here we have linking the XML file for which we have to write the Java code.
  • Line 27 – 32: Here we have done TypeCasting of each into TextView, EditText and Button.
  • As we  know findViewById returns View, which is needs to be Typecast into respective Element.
  • Line 34: Here we make reference of Intent which is inetnt2.
  • We call the getIntent() method. getIntent() method return object, so we store into intent2.
  • Line 36 – 38: We can extract data from Intent object by using Intent.getString(key) method.
  • Line 40-43: We can also extract data from  Intent object, by using Bundle class.
  • After creating Bundle class reference, we can use Bundle methods, bundle.getString(key) to get the data from Intent.
  • Line 45-46: Here we set data into respective TextViews.
  • Line 48: Here we created the click Listener for btn2.
  • As we want to send data back to Activity1, we write code under Line 51 – 53.
  • Line 51: We created the reference of Intent as intent2 and initialized with default constructor.
  • Line 52: Here we had used putExtra() method to send our data.
  • Line 53: Here we set result for Activity1 using setResult() method.
  • Line 55: We end the activity using finish() method.

Finally!

We are ready to run our TransferDataBetweenActivties android app. To run our app I used Genymotion Emulator as it’s fast. Also its performance is better than default Android emulator.

If you have not already installed I highly recommend Genymotion emulator. You can install from here: Android Emulator Genymotion Step by Step Installation. If you face any errors while installing android studio this is a good reference point.

Running Sample Android App

  • Click on Genymotion device manager. After selecting your custom device in Genymotion device manager window, click START.
  • Click on Run.
  • Choose Genymotion device and click OK.
  • Now you can see yourTransferDataBetweenActivties app running.
Android Development Tutorial : Send Data Between Activities using Intent

Output1

Android Development Tutorial : Data Transfer Between Activities using Intent

Output2

Android Development Tutorial : Data Transfer Between Activities using Intent

Output3

 

Congratulations! If you reach this point means you have followed all the steps correctly and android app is running fine. If you are a beginner android developer and learning android, I recommend go through below android tutorials to get started.

If I missed any point or you have any doubt regarding this blog just let me know in the comment section.

If you like my article please like our Facebook page and also follow us on Twitter so that you get regular updates. For regular updates you can also subscribe to www.hackpundit.com with your email.

Please also share on Facebook and Twitter to help other android developers. Thank You..!