CodeNoob
CodeNoob

Reputation: 757

Use implicit intent to launch another activity in the same app

So recently at an Interview I was asked if we can use implicit intent to launch Activity B from Activity A. From what I understand that we can do this but it would lead to OS showing a popup and user selecting which app (if multiple) to chose from. So in what case can this be useful to launch another activity within same app with implicit intent.

Upvotes: 1

Views: 1199

Answers (3)

Aldo Wachyudi
Aldo Wachyudi

Reputation: 17991

So recently at an Interview I was asked if we can use implicit intent to launch Activity B from Activity A

Yes, we can navigate between activities using implicit intent.

From what I understand that we can do this but it would lead to OS showing a popup and user selecting which app (if multiple) to chose from.

Correct. Implicit in this context means we don't know which application will handle our intention. This can be useful when we have multiple apps for handling a similar task. For example, you have multiple apps to manipulate images, send an email, opening a file, and so on.

However, we can make an implicit intent to open only one app by passing the package id.

val action = "com.example.myapp.myaction"
val packageId = "com.example.myapp"
val intent = Intent(action, packageId)

This is possible because an app in Play Store is guaranteed to have a unique package id (also called application id). So, if you publish your app under application id of "com.example.myapp", your user will only open your app.

So in what case can this be useful to launch another activity within same app with implicit intent.

This technique is commonly used for navigation between Activity in a multi-module projects (modularization). In a multi-module projects, the feature modules (the one that has an Activity) don't have dependencies to each other. Which means, you can't use an explicit intent.

// Will cause ClassNotFoundException since 
// AnotherActivity does not exist!
val intent = Intent(context, AnotherActivity::class.java)

This is where implicit intent comes in handy. Feature A can use an implicit intent to launch feature B (AnotherActivity).

<activity android:name="AnotherActivity" android:exported="false">
    <intent-filter>
        <action android:name="com.example.myapp.myaction"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</activity>

Notes:

  • There is another way to navigate between Activity in a feature modules that uses an interface of the destination, and the destination Activity will implement the interface. But it's irrelevant for this question.

  • If you use Single Activity Architecture, where each screen is represented by Fragment (not Activity), you can use deep link with Jetpack Navigation library.

Upvotes: 0

Dimitrios Tsigouris
Dimitrios Tsigouris

Reputation: 41

You can define your own Implicit Intent and also define an Intent Filter.

Example: In your app's AndroidManifest.xml:

 <activity
      android:name=".MyActivity"
      android:launchMode="singleTop">
   <intent-filter>
      <action android:name="com.my.app.NAME" />
      <category android:name="android.intent.category.DEFAULT" />
      <data android:mimeType="text/plain" /> 
   </intent-filter>
</activity>

So your activity listens for a specific intent action.

Here is how your app (or any other) can open this activity with an Implicit intent:

          val sendIntent = Intent().apply { 
            action = "com.my.app.NAME"
            type = "text/plain"
            putExtra("EXTRA_KEY","some argument I want to pass as an Extra")
          }

Before you send a custom implicit intent, you should always verify there is an Activity that can handle it, via the package manager:


// Verify that the intent will resolve to an activity
if (sendIntent.resolveActivity(context.packageManager) != null) {
    startActivity(sendIntent)
}

Upvotes: 3

Abdul Mateen
Abdul Mateen

Reputation: 1674

I think this can be done by defining custom intent-filter with custom action that is recognized by the Activity B. Let's say you want to launch Activity B from Activity A, you will create an implicit intent with the specified action and it will then launch Activity B. I cannot think of a concrete use-case for this though.

Upvotes: 0

Related Questions