Online Tutorials & Training Materials | STechies.com
Register Login

Explicit and Implicit intent in Android

|| || 0

Explicit and Implicit intent in Android
Stechies

Explicit intent is one that you use to launch a specific app component, such as a particular activity or service in your app.An implicit intent specifies an action that can invoke any app on the device able to perform action.

How to use explicit and implicit intent in a device

Please follow the steps below to create explicit intent:

Step 1: First you need to create a new app by clicking on the option “Start a new project”

Name the app as “AppOne”.

Next, you need to select the option “blank activity”

A new project is now created.

Step 2: You have to click on the “large text” option and drag the pointer on the default screen.

Step 3: you have to click on the “button” option and drag the pointer on the default screen.

Step 4: go to the text area to change the text of the “large text” to “Main Activity App One”

Step 5: Under “Button” section, change the text of the “Button” to “Open Activity One”

Now you need to create new an activity which will open when you click on the button “Open Activity One”.

Step 6: In order to create a new activity, click on the option “app”, then select the option “New”, select option “Activity” and finally select the option “Blank Activity”.

Step 7: you have to name this activity as “ActivityOneAppOne”

Step 8: Delete the text “hello world” and replace it in the text area “Activity One App One”

Step 9: You need to go to the mainactivity.xml file to open this activity.

Step 10: Next go to the “button” component and add an attribute called “android: onClick” and then you name a method for example “openOne”.

Step 11: You need to create this method in your mainactivity.java file

Step 12: here you create a method which will be “public void openOne”. Remember that here you use the same name as the method name. Put an argument adjacent to the method which is “View view” and inside this method create an instance “Intend_intend = new Intend”. Place two arguments. The first argument is the context “this” and second is the activity class name “ActivityOneAppOne.class”. This will create an intent object which you can call from the method called start activity. So write “start activity” and pass the intent object adjacent to it like this start activity (intent);” this is going to open your first activity using your main activity.

Now when you run this program and click on the button “Open Activity One”, it is going to open the Activity One in App One “Activity One App One”.

This was an example of explicit intent where you can open an activity within an app.

Please follow the steps below to create implicit intent:

Step 1: first you need to create a new app and name it as AppTwo

Your AppTwo is created

Step 2: here you design the app in order to open the activity from your first app – AppOne. For that, you have to click on the “button” option and drag the pointer on the default screen.

Step 3: go to the text area and change the text from “button” to “Open Activity One from App One”

Step 4: To open activity one from app one using your app two, first you need to go to your last project and open manifestfile.xml file.

Whenever you create an activity, you will find that some extra tags are created for your activity.

In order to be able to open this activity from your other app, you need to copy the filter intent from the main activity and paste it inside the activity as shown below.

Step 5: you need to change the android name of the two tags –“action” and “category” – in the activity area. In the action tag you need to give the android name of your activity. To do so, you need to go the activity one in app one. Go the activity_activity_one_app_one.xml file of the activity one in app one and copy the package name form this section.

Now you need to paste this package name as your activity under the “action” tag. Pasting the package name in this section will enable your app to be visible on every app of your device.

Step 6: In order to allow other device to access your activity you have to have the “category” as “DEFAULT. Now your activity is visible to other apps within your device.

Step 7: Now go back to your AppTwo and enable this “button” which we have already created in AppTwo to open the activity from app one. To do this you need to go the text area and add an attribute under the button section. The attribute will be: android: onClick= “openAct1FromApp1”.



Now go to the method name option “openAct1FromApp1” and go to the Main Activity.java file of AppTwo. Create a new method “public void openAct1FromApp1 (View view)”. Then create an intent inside the method “intent intent = new Intent (); and paste the package name of AppOne instead of giving a specific context in the bracket of the method. Now write start activity and pas on the intent here “start activity (intent).

Once you are done with this, you can run your app.

Now when you click this “Open Activity One From App One” you will see that “Activity One App One” is opened.

This was an example of an implicit intent where you open an activity from some other app within your device.

Read Next About How to add Up button in Action Bar


Related Articles