简体   繁体   中英

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.

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)
}

我认为这可以通过使用Activity B 识别的自定义action定义自定义intent-filter来完成。假设您想从Activity A 启动Activity B,您将使用指定的action创建一个implicit intent ,然后它将启动Activity B。不过,我想不出具体的用例。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM