如果应用程序已通过深层链接打开,则Android深层链接无效

Shi*_*ing 52 android deep-linking

如果已通过深层链接打开应用程序,则深层链接不起作用.

但是,如果我打开应用程序而不是通过触发深层链接,例如单击应用程序图标以打开应用程序.然后在之后触发deeplink将始终有效.


这里有详细信息:

所以我在AndroidManifest中设置了这样的活动,即LaunchActivity.

<activity
    android:name="some.package.name.LaunchActivity"
    android:screenOrientation="portrait"
    android:theme="@style/Theme.SomeTheme">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="dlscheme" android:host="dlhost" />
    </intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)

在LaunchActivity中,我会在onCreate()中打印一个日志,表明它已存在.

我用了

adb shell am start -W -a android.intent.action.VIEW -d "dlscheme://dlhost/param" some.package.name
Run Code Online (Sandbox Code Playgroud)

测试深层链接.

随着应用程序被杀,我使用了上面的命令.它可以打开应用程序并路由到正确的活动,没问题.并有以下日志.

adb shell am start -W -a android.intent.action.VIEW -d "dlscheme://dlhost/param" some.package.name
Starting: Intent { act=android.intent.action.VIEW dat=dlscheme://dlhost/param pkg=some.package.name }
Status: ok
Activity: some.package.name/.activity.LaunchActivity
ThisTime: 898
TotalTime: 898
WaitTime: 919
Complete
Run Code Online (Sandbox Code Playgroud)

但是,如果我再次输入相同的命令,而不会杀死应用程序.它只会打开应用程序,但它不会打开正确的活动,并生成以下日志.

adb shell am start -W -a android.intent.action.VIEW -d "dlscheme://dlhost/param" some.package.name
Starting: Intent { act=android.intent.action.VIEW dat=dlscheme://dlhost/param pkg=some.package.name }
Warning: Activity not started, its current task has been brought to the front
Status: ok
Activity: some.package.name/.activity.LaunchActivity
ThisTime: 0
TotalTime: 0
WaitTime: 6
Complete
Run Code Online (Sandbox Code Playgroud)

这条额外的线

Warning: Activity not started, its current task has been brought to the front
Run Code Online (Sandbox Code Playgroud)

我实际上也尝试过使用这个chrome意图的网站:

intent://dlhost/param#Intent;scheme=dlscheme;package=some.package.name;end
Run Code Online (Sandbox Code Playgroud)

它会表现得一样.

Mic*_*rla 12

在项目的清单文件中,您需要将跟随添加到主要活动中.

android:launchMode="singleTask"
Run Code Online (Sandbox Code Playgroud)

所以,在清单中,你会得到类似下面的内容:

<activity android:name="some.package.name.LaunchActivity" 
      android:launchMode="singleTask">
      android:screenOrientation="portrait"
      android:theme="@style/Theme.SomeTheme">
          <intent-filter>
              <action android:name="android.intent.action.MAIN" />
              <category android:name="android.intent.category.LAUNCHER" />
          </intent-filter>
          <intent-filter>
              <action android:name="android.intent.action.VIEW" />
              <category android:name="android.intent.category.DEFAULT" />
              <category android:name="android.intent.category.BROWSABLE" />
              <data android:scheme="dlscheme" android:host="dlhost" />
          </intent-filter>
 </activity>
Run Code Online (Sandbox Code Playgroud)

基本上它的作用是创建一个新任务,一个新实例将作为根任务推送到任务.但是,如果任何任务中存在任何活动实例,系统会通过onNewIntent()方法调用将意图路由到该活动实例.在此模式下,活动实例可以推送到同一任务.如果用户从当前活动中单击BACK键,系统将使用户返回上一个活动.

另一方面,singleTop如果活动实例已经存在于当前任务的顶部并且系统将意图路由到此活动,则不会创建新实例,因为它将触发onNewIntent()方法而不是创建新对象.

更多信息可以在这里找到.

  • 感谢您的回答(:但让我感到困扰的是,通常可以打开多个活动,即如果您启动您的应用程序,然后打开深度链接,然后在您的应用程序中打开不同的屏幕,然后再次打开深度链接工作正常,但是如果您第一次启动活动是深层链接活动,则第二次将不起作用。就像 android 出于某种原因阻止它的娱乐。 (3认同)
  • 嘿!谢谢,这是解决此问题的巧妙方法,谢谢。但是你能帮我理解为什么会这样吗?为什么 android 需要 `singleTask` 才能使其深层链接路由活动正确运行?如果我从 deeplink 打开应用程序,我的 DeeplinkActivity 将成为该任务的根目录。但是,如果我从中打开另一个活动,并完成 DeeplinkActivity,这不会使我的新活动成为根吗?如果是这样,为什么仍然无法重新创建 DeeplinkActivity? (2认同)

res*_*218 10

在项目的清单文件中,您需要将跟随添加到主要活动中.

android:launchMode="singleTask"
Run Code Online (Sandbox Code Playgroud)

并处理内部的深层链接 onNewIntent()

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recipe);
    onNewIntent(getIntent());
}

protected void onNewIntent(Intent intent) {
    String action = intent.getAction();
    String data = intent.getDataString();
    if (Intent.ACTION_VIEW.equals(action) && data != null) {
        String recipeId = data.substring(data.lastIndexOf("/") + 1);
        Uri contentUri = RecipeContentProvider.CONTENT_URI.buildUpon()
                .appendPath(recipeId).build();
        showRecipe(contentUri);
    }
}
Run Code Online (Sandbox Code Playgroud)


DDs*_*six 8

android:launchMode="singleTop"在LaunchActivity活动代码中添加清单


use*_*843 6

我发现添加android:launchMode="singleTask"作品.singleTop不适合我.