如果应用程序关闭,深层链接不会正确重定向

Posted

技术标签:

【中文标题】如果应用程序关闭,深层链接不会正确重定向【英文标题】:Deeplink isn't correctly redirect if app is closed 【发布时间】:2021-09-01 13:59:09 【问题描述】:

我正在使用 Jetpack Navigation 进行深度链接。如果之前未打开应用程序,则深层链接不会正确重定向。但是,如果应用程序存在于内存中,则应用程序将进入前台并且深度链接正确打开。我有 2 个活动。一个 registerActivity 另一个 mainActivity。 我想在 mainActivity 的片段中使用深层链接。

这是 Jetpack Navigation 中的错误还是我在设置时忘记了什么?

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.life4.ecommerce">


<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

<application
    android:name=".ECommerceApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.ECommerce"
    android:requestLegacyExternalStorage="true"
    android:usesCleartextTraffic="true">

    <!--
         The API key for Google Maps-based APIs is defined as a string resource.
         (See the file "res/values/google_maps_api.xml").
         Note that the API key is linked to the encryption key used to sign the APK.
         You need a different API key for each encryption key, including the release key that is used to
         sign the APK for publishing.
         You can define the keys for the debug and release targets in src/debug/ and src/release/.
    -->
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

    <activity android:name=".view.RegisterActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".view.MainActivity"
        android:windowSoftInputMode="adjustResize">
        <nav-graph android:value="@navigation/nav_main" />
    </activity>
</application>

</manifest>

这是导航图的一部分

<fragment
    android:id="@+id/productDetailsFragment"
    android:name="com.life4.ecommerce.view.ProductDetailsFragment"
    android:label="fragment_product_details"
    tools:layout="@layout/fragment_product_details">
    <argument
        android:name="productID"
        app:argType="integer" />
    <action
        android:id="@+id/action_productDetailsFragment_to_profileFragment"
        app:destination="@id/profileFragment" />
    <action
        android:id="@+id/action_productDetailsFragment_to_imageDetailsFragment"
        app:destination="@id/imageDetailsFragment" />
    <deepLink
        android:id="@+id/deepLink2"
        app:uri="example.com/pro/productID" />
</fragment>

【问题讨论】:

"如果之前没有打开过应用,Deeplink 就不会正确重定向。" - 那么会发生什么?您的活动是否开放? 是的,只有开放的活动 【参考方案1】:

如果您的导航图内部包含 sum 包含图,则可能会发生这种情况。 使用深度链接意图数据打开必须具有平面图,您的应用可以在片段中导航。 例如,您的问题的另一种解决方案是使用额外的而不是数据 当您想以可以激活片段深层链接的意图开始您的活动时,您可以使用额外和操作视图。 请参阅下面的代码以更好地理解解决方案。 首先,这是必须存在平面导航图的硬解决方案

val intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.data=Uri.parse("your deeplink url")

您可以使用下面更好的代码,并且您的导航图不会改变

val intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.putExtra(INTENT_KEY,"your deeplink url")

你可以像这样在你的活动中处理你的意图数据

intent?.getStringExtra(INTENT_KEY)?.let 
      navController.navigate(Uri.parse(it))

【讨论】:

正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。

以上是关于如果应用程序关闭,深层链接不会正确重定向的主要内容,如果未能解决你的问题,请参考以下文章

从 facebook 重定向到 ios 应用程序 - 深层链接

使用 branch.io 来自 Play 商店的 Android 深层链接

Http 302 重定向到深层链接在 Android 中不起作用

Web 应用程序和深层链接

使用路由器链接进行路由时,它会正确创建 URL,但不会重定向到组件并显示数据?

从重定向而不是链接打开Android应用程序