我已经使用 Jetpack Navigation 在我的应用中实现了深度链接。我的应用程序遵循单一活动架构。片段之间的导航由导航图组织。
我发现了一个案例,其中两者 onCreate
和 onNewIntent
回调都被调用。这不应该发生。让我解释一下用例:
上面用例的 Logcat 看起来是这样的(按正确的顺序):
因此应用程序一次创建 MainActivity
的两个实例,并在同一个实例中触发 onCreate
和 onNewIntent
回调。
当应用未关闭而用户在浏览器中时,logcat 看起来像这样:
singleTask
和 singleInstance
启动模式都没有改变任何东西。
我的问题是:
onCreate
和 onNewIntent
回调?MainActivity
实例?我会非常感谢回答我问题的人。在这里我添加我的代码:
清单:
<activity
android:name=".main.MainActivity"
android:launchMode="singleTop">
<nav-graph android:value="@navigation/nav_graph" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
导航图:
<fragment
android:id="@+id/paymentRedirectFragment"
android:name="com.example.myapp.PaymentRedirectFragment">
<action
android:id="@+id/action_paymentRedirectFragment_to_transactionCompletionFragment"
app:destination="@id/transactionCompletionFragment"
app:popUpTo="@id/paymentRedirectFragment"
app:popUpToInclusive="true" />
<deepLink
android:id="@+id/web_payment_deep_link"
app:uri="com.example.myapp://foo/bar/this_is_just_example" />
</fragment>
主要活动:
private val navController by lazy {
findNavController(R.id.mainNavigationFragment)
}
override fun onCreate(savedInstanceState: Bundle?) {
setTheme(R.style.AppTheme)
super.onCreate(savedInstanceState)
setSupportActionBar(toolbar)
NavigationUI.setupWithNavController(
binding.bottomNavigation,
navController
)
}
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
mavController.handleDeepLink(intent)
}
对浏览器的调用意图:
try {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))
} catch (ex: ActivityNotFoundException) {
showSnackBar(R.string.browser_not_found, binding.root)
}