带有导航库的 Android 应用静态快捷方式

标签 android kotlin android-architecture-components android-jetpack android-appshortcut

我的应用程序有一个静态快捷方式,我正在使用导航架构组件。

我正在使用以下实现,但它不适用于 Android 9。

在我的 MainActivity 中:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    setupToolbar()
    setupNavigationDrawer()

    checkPermissions()

    if (intent.action == MoneyBook.SHORTCUT_ACTION_ADD_BOOKENTRY) {
        findNavController(R.id.nav_host_fragment).popBackStack(R.id.action_entriesFragment_to_newBookEntryFragment, false)
    }
}

快捷方式.xml:
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:enabled="true"
        android:icon="@drawable/ic_shortcut_addbookentry"
        android:shortcutId="shortcut_addbookentry"
        android:shortcutLongLabel="@string/shortcut_addbookentry_label_short"
        android:shortcutShortLabel="@string/shortcut_addbookentry_label_short">

        <intent
            android:action="at.guger.moneybook.action.ADD_BOOKENTRY"
            android:targetClass="at.guger.moneybook.activity.MainActivity"
            android:targetPackage="at.guger.moneybook.dev" />
    </shortcut>
</shortcuts>

在调试应用程序时,我注意到 findNavController(...)方法被调用,但屏幕上没有任何 react 。

此外,我不会覆盖 onStart(...)onResume(...) .

有没有给定的方法来实现带有导航组件的应用程序快捷方式,或者我在这里做错了什么?

最佳答案

导航不支持 intent.action 的深层链接独自的。但是,您可以添加 android:data元素到您的<intent>元素,然后是 add a implicit deep link到您的导航图。通过这样做,您不必编写任何特定代码来处理 Intent。

例如,更改您的 shortcuts.xml添加 android:data属性:

<intent
        android:action="at.guger.moneybook.action.ADD_BOOKENTRY"
        android:targetClass="at.guger.moneybook.activity.MainActivity"
        android:targetPackage="at.guger.moneybook.dev"
        android:data="moneybook://add_book_entry" />

在您的导航图 XML 文件中,添加 <deepLink>相同 URL 的元素:
<fragment
  android:id="@+id/add_book_entry_fragment"
  android:name=".AddBookEntryFragment">
  <deepLink app:uri="moneybook://add_book_entry" />
</fragment>

关于带有导航库的 Android 应用静态快捷方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54599672/

相关文章:

java - Android - 重量选择器

android - 如何使用 SpannableStringBuilder 缩放多个跨度?

android - Anko协程kotlin中的Deferred是什么?

java - Android 实时数据 : Transformation switchMap: Apply filter on the original list and show the filtered data

android - 更改抽屉项目android的文本大小

android - 使用 SSL 时 HttpClient 和 HttpsURLConnection 返回不同的证书

java - 如何在Kotlin中使用注释TypeDefs?

android - 在 MVVM 应用程序中公开 XML 布局中的 Activity 是一种不好的做法吗?

android - 由于缺少方法导致的预启动报告失败(在 com.google.android.apps.mtaas.crawler-1/base.apk 中)

android - 在 SurfaceTexture 中渲染时,谁会旋转相机的帧?