Android Explicit Intent

 

  • Android Explicit Intent is used to invoke components such as Activity / Service / Broadcast Receiver of our own application, e.g. Launching Dashboard Activity with Explicit Intent, when user clicks on submit button from Login Activity. 
  • Android Explicit Intent designates target component by it’s name (i.e. specifying Activity Class name as 2nd parameter while declaring intent).
  • Android Explicit Intent are also used to pass data from one Activity to another.

 

Android Explicit Intent

 



Download
Source Code : Explicit Intent.zip


 

Now Let us work on Project : (Refer – Configuring Existing AndroidStudioProject) Explicit Intent 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/ExplicitIntent/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 :

  • Activity1.java  which will collect data inside edit-text entered by user & forward to Activity2.java using Explicit Intent.
  • Activity2.java which is invoked by Activity1.java, also it will receive data passed by Activity1.java.

 

 

Activity1.java

Explaining above Code :

  • Line 15 We have declared 2 string variable  ( TAG_NAME & TAG_NUMBER )  containing respective identifier name ( tg_name & tg_number ). This concept is called name-value pair, where we declare a variable holding identifier name inside it, using this identifier name we will obtain the actual value wherever required. You will also see that we have declared these variables static, by this way we can make our variable usable in other Activities wherever required. You will see how we used these variables in our Activity2.
  • Line 27 We have our button-click handler for the button, which will start our Activity2.
  • Line 29 We make an instance of Intent class and pass 2 parameters  :  this  specifying the context of our App.  &  Activity2.class specifying to invoke our Activity2.java file when startActivity() is called.
  • Line 30 Using above instance of intent we send data to our next Activity2 using method putExtra(). 1st parameter is TAG_NAME  ( name-value pair we declared at Line15 )  &  second is edit_txt1.gettext().toString() which extracts data from our Edit Text which we want to pass over to Activity2.
  • Line 31 In same way as above Line-30 we send our value of 2nd EditText to Activity2 using name-value pair TAG_NUMBER.
  • Line 32 Now we launch our Activity2 using method  startActivity()
  • Line 35 If we don’t require our Activity1 any more, than use method finish() in order to close it, else leave it as a comment.

 

 

Activity2.java

Explaining above Code :

  • Line 24 We have declared Intent using  getIntent() which will receive data passed by Activity1. ( Note that there is no compulsion to keep same name of Intent in Activity1 & Activity2, you can use your desired name )
  • Line 27 So as to catch response of putExtra() method that we used in our Activity1, we have to call getStringExtra(). To this we pass name-value pair that we declared at Line15 of Activity1 ( Activity1.TAG_NAME i.e. from Activity1 file & name-value pair : TAG_NAME ). This will result into a string value which we sent from Activity1. We store this value in variable to the left of the assignment operator (=).
  • Line 28 In same way we receive 2nd value using getStringExtra( Activity.TAG_NUMBER ) & store it inside variable number.
  • Line 31-32 We simply set the values received from Activity1 to 2-TextViews declared previoously.

 

 

activity_1.xml : Layout file

  • Inside our activity_1.xml we have added  2-EditText which will collect 2 values entered by user & 1- Button which will invoke  methods containing Explicit Intent.

 

 

 activity_2.xml : Layout file

  • Inside our activity_2.xml we have simply added  2-TextViews which will display collected information sent from Activity1 using Explicit intent.

 

Final Output :

Android Explicit Intent

Activity1.java

Android Explicit Intent

Activity2.java

 

 

 

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.