0

I have a share fragment receives a youtube share URL via intent and uses navigation the flow is:

ScanFragment -> PlaylistFragment
             -> VideoFragment

So the scan fragment parses the url and loads the data and then forwards to the relevant fragment to display the data

So if a user presses home on the VideoFragment or PlaylistFragment then the activity stay alive and navigation is not at its initial point ScanFragment

So if the user shares another URL then the intent comes via onNewIntent and the navigation needs to be reset so the URL can be routed to the scan fragment;

How can I ensure the navigation is reset to its initial state? I have tried navigating manually to scan fragment - but there seems to be latency and also the back stack isn't correct.

The nav is initialized in the NavHostFragment(app:navGraph)

How can i force the navigation to reset in onNewIntent?

EDIT: so there where a couple of things i missed here: https://developer.android.com/guide/navigation/navigation-deep-link

I updated my nav graph to add the deepLink for my scan fragment

<fragment
        android:id="@+id/share_navigation_scan"
        android:name="uk.co.sentinelweb.cuer.app.ui.share.scan.ScanFragment"
        android:label="@string/sharenav_title_scan"
        tools:layout="@layout/fragment_scan">
        <action
            android:id="@+id/action_goto_playlist"
            app:destination="@id/share_navigation_playlist"
            app:launchSingleTop="true"
            app:popUpTo="@id/navigation_playlist"
            app:popUpToInclusive="true" />

        <action
            android:id="@+id/action_goto_playlist_item"
            app:destination="@id/share_navigation_playlist_item_edit"
            app:launchSingleTop="true"
            app:popUpTo="@id/navigation_playlist_item_edit"
            app:popUpToInclusive="true" />

        <deepLink app:uri="https://www.youtube.com/watch?v={id}" />
        <deepLink app:uri="https://youtube.com/watch?v={id}" />
        <deepLink app:uri="https://m.youtube.com/watch?v={id}" />
        <deepLink app:uri="https://youtu.be/{id}" />
        <deepLink app:uri="https://www.youtube.com/playlist?list={id}" />
        <deepLink app:uri="https://youtube.com/playlist?list={id}" />
    </fragment>

and i have removed android:launchMode="standard" - seems like that should be right for navigation though.

and my onNewIntent

override fun onNewIntent(intent: Intent) {
     super.onNewIntent(intent)
     navController.handleDeepLink(intent)
     checkIntent(intent)
}

checkIntent tries a few different way of ggetting the url from the intent and passing it down to the scan fragment

but still when i checkIntent the fragment is not ScanFragment -

I also tried removing checkIntent and getting the url in the onStart of ScanFragment - but still no joy.

So event with the handleDeepLink in onNewIntent (which i shouldnt need anyway according to the docs if it's standard launchmode ) - still the nav doesnt return to scan fragment for subsequent deeplink launches after the first one.

activity manifest entry

<activity
            android:name=".app.ui.share.ShareActivity"
            android:exported="true">

            <nav-graph android:value="@navigation/deeplink" />

            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.BROWSABLE" />
                <category android:name="android.intent.category.DEFAULT" />

                <data android:mimeType="text/plain" />

                <data android:scheme="http" />
                <data android:scheme="https" />
                <data android:host="youtu.be" />
                <data android:pathPattern="/" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />

                <data android:mimeType="text/plain" />

                <data android:host="youtu.be" />
                <data android:path="/" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />

                <data android:mimeType="text/plain" />

                <data android:host="youtube.com" />
                <data android:host="www.youtube.com" />
                <data android:host="m.youtube.com" />

                <data android:path="/watch" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />

                <data android:mimeType="text/plain" />

                <data android:host="youtube.com" />
                <data android:host="www.youtube.com" />
                <data android:host="m.youtube.com" />

                <data android:path="/playlist" />
            </intent-filter>
        </activity>
siliconeagle
  • 7,379
  • 3
  • 29
  • 41
  • If you don't have any `launchMode` on your activity (which is what [the documentation specifically recommends](https://developer.android.com/guide/navigation/navigation-deep-link#handle)), then this is handled automatically for you. Why are you adding a launchMode at all? – ianhanniballake Jul 30 '22 at 14:07
  • i did have `android:launchMode="standard"` - but i removed it and the behaviour is still the same. – siliconeagle Jul 31 '22 at 02:55
  • so i missed some things here but it still doesn't work - have updated above – siliconeagle Jul 31 '22 at 03:10
  • `onNewIntent` isn't even called when using the default `standard` launchMode. If that's still being called, them something else is wrong with your setup. – ianhanniballake Jul 31 '22 at 04:17
  • yes i don't understand - its definitely disabled in the manifest. There are intent filters on the `activity`. i out he activity config above. – siliconeagle Jul 31 '22 at 10:23

0 Answers0