Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android App Static Shortcut with Navigation Library

My app has one static shortcut, and I'm using the navigation architecture component.

I'm using the following implementation but it does not work on Android 9.

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

shortcuts.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>

When debugging the app, I noticed that the findNavController(...) method is called, but nothing happens on the screen.

Further, I do not override onStart(...) or onResume(...).

Is there any given way to implement app shortcuts with navigation component or am I doing something terribly wrong here?

like image 919
the_dani Avatar asked Feb 08 '19 20:02

the_dani


People also ask

How do I create a custom shortcut on Android?

Find the app you want to create a shortcut for and long-press on its icon. Tap Add to home. On some Android devices, you will need to long-press the icon and drag the app to the home screen. The app's icon should then appear in the top-right corner of your Home screen on your Android tablet or smartphone.

What are dynamic shortcuts?

Static shortcuts are defined in a resource file that is packaged into an APK or app bundle. Dynamic shortcuts can be pushed, updated, and removed by your app only at runtime. Pinned shortcuts can be added to supported launchers at runtime, if the user grants permission.


1 Answers

Navigation does not support deep linking by intent.action alone. However, you can add an android:data element to your <intent> element and then add a implicit deep link to your navigation graph. By doing this, you won't have to write any specific code for handling the Intent.

For example, change your shortcuts.xml to add the android:data attribute:

<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" />

and in your navigation graph XML file, add a <deepLink> element for the same URL:

<fragment
  android:id="@+id/add_book_entry_fragment"
  android:name=".AddBookEntryFragment">
  <deepLink app:uri="moneybook://add_book_entry" />
</fragment>
like image 54
ianhanniballake Avatar answered Oct 17 '22 12:10

ianhanniballake