Android Intent Result Example

 

Intents are mainly used for communication between 2-activities or to pass data between them. Whenever we start a new Activity using Intent, that Intent can also return some result to our older Activity from newly created Activity. This process is called receiving intent result.

 

Android Intent Result Memory Stack

  • Many a times we might have various operations to be carried out from our Activity. Adding all operations in same activity will take the charm of User-Interface. For that we might have multiple Activities computing the operations separately and sending back result to the main Activity.
  • Say for e.g. you start the Numeric Key-pad to call a person, in order find number of that person you might invoke contact book. On selecting the contact you’ll receive the contact details as a result back to your Numeric Key-pad.
  • Another Intent Result Example : while using any App you want to capture a photograph, the application invokes the default Camera App. You click a picture & that App receives an image as intent result from the Camera App.

Android Intent Result - Working

 



Download
Source Code : Intent Result Data.zip


 

Now Let us work on Project : (Refer – Configuring Existing AndroidStudioProject) Intent Result Data which you might have downloaded from above or you can follow the scripts below by copying it to implement  in your Project. To launch Downloaded project, Extract the zip, copy it into AndroidStudioProjects. Now from Android Studio click open Existing Android Studio Project and select the path to extracted file we just copied in AndroidStudioProjects directory. If Sync SDK path prompt comes just click OK and you are ready to work. If still you get error check for AndroidStudioProjects/IntentResultData/app/build.gradle , it will contain different SDK Version. Change it to SDK Version which you have.

 

Over here we will work on 2 main files :

  • MainActivity1.java  which will invoke MainActivity2.java & waits till  MainActivity2.java  responds either with Success or Failure as Intent Result.
  • MainActivity2.java which is invoked by MainActivity1.java, sends Intent Result back to MainActivity1.java, when user clicks the Button.

 

 

MainActivity1.java

Explaining above Code :

  • Line 13 We have declared integer variable REQ_CODE which will help us identify incoming intent result belongs to which Activity in case we have multiple activities. We have declared its value 1, which means that only Activity returning same REQ_CODE for intent result will be kept rest will be ignored.
  • Line 16 We have declared name-value pair TAG_MESSAGE which will be used for exchanging data using Intent. And we have made it static since we will also need to use it in MainActivity2.java.
  • Line 34 Whenever we click our button this method will be invoked.
  • Line 37 We have declared an Explicit Intent passing context & MainActivity2.class as 2 parameters.
  • Line 40 startActivityFor Result() starts MainActivity2 with intention of receiving intent result. At this point our MainActivity1 will be sent to background & MainActivity2 will be started & brought in front.
  • Line 48 Whenever our MainActivity2 will send response onActivityResult() will be invoked with 3 parameters passed by MainActivity2, viz : requestCode & resultCode – to confirm that proper intent result is received at MainActivity1 & Intent data which is the data sent from MainActivity2 in form of intent result.
  • Line 51 We compare  requestCode with resultCode if both match then we assume successful reception of intent result else we give error message.
  • Line 54 using data.getStringExtra(TAG_MESSAGE) we extract the message from intent result sent by MainActivity2.

 

 

MainActivity2.java

Explaining above Code :

  • Line 24 After filling the EditText we submit it by clicking send button, our run method will be called which deals with sending intent result back to MainActivity1.
  • Line 32 We have declared intent using which we will send back intent result to MainActivity1. ( See that we haven’t passed any parameters to this intent )
  • Line 35 Using the above intent instance we attach data of our EditText using method putExtra().
  • Line 38 We send intent result back to MainActivity1 with resultCode REQ_CODE = 1 i.e. success using setResult().

 

 

activity_main1.xml : Layout file

  • Inside our activity_main1.xml we have added 1st TextView to display static info.,  2nd Button to start MainActivity2 &  3rd TextView to display received intent result.

 

 

 

 activity_main2.xml : Layout file

  • Inside our activity_main2.xml we have simply added  1-EditText to collected message from user & 1-Button to send intent result back to MainActivity1.

 

 

Final Output :

Android Intent Result

MainActivity2.java

Android Intent Result

MainActivity1.java (Receiving Intent Result)

 

 

Report Errors + Bugs & Become Insider for Nestedif.com

We would like to hear you, if you find any error or misspelled phrase while reading our tutorials. By reporting mistakes through email to insider@nestedif.com you could help other peers.