android / architecture-components-samples

Samples for Android Architecture Components.
https://d.android.com/arch
Apache License 2.0
23.45k stars 8.28k forks source link

handle deeplink within bottom navigation #840

Closed akshay253101 closed 2 years ago

akshay253101 commented 4 years ago

Using handle deeplinks restarts the app to create correct backstack but to open deeplink within bottom navigation without restarting app.


fun BottomNavigationView.navigateDeeplink(
        navGraphIds: List<Int>,
        fragmentManager: FragmentManager,
        containerId: Int,
        uri: Uri) {
    navGraphIds.forEachIndexed { index, navGraphId ->
        val fragmentTag = getFragmentTag(index)

        // Find or create the Navigation host fragment
        val navHostFragment = obtainNavHostFragment(
                fragmentManager,
                fragmentTag,
                navGraphId,
                containerId
        )
        // Handle deeplink
        val canHandleDeeplink = navHostFragment.navController.graph.hasDeepLink(uri)

        if (canHandleDeeplink) {
            if (selectedItemId != navHostFragment.navController.graph.id) {
                selectedItemId = navHostFragment.navController.graph.id
            }
            navHostFragment.lifecycleScope.launchWhenResumed {
                // Wait for fragment to restore state from backStack
                // otherwise navigate will be ignored
                // Ignoring navigate() call: FragmentManager has already saved its state
                navHostFragment.navController.navigate(uri)
            }
        }
    }
}
googlebot commented 4 years ago

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

:memo: Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

akshay253101 commented 4 years ago

@googlebot I signed it!

googlebot commented 4 years ago

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

nijat-ahmadli commented 4 years ago

@akshay253101 In addition to this I had to prevent navController.handleDeepLink(intent) to be called automatically on onCreate() of the Activity with following code:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    val uri = intent.data
    intent.data = null
}

Otherwise, my Activity still was getting created twice. For more details please refer to https://stackoverflow.com/a/63032780/3873867

akshay253101 commented 4 years ago

@nijat-ahmadli The above solution has an issue that it will create multiple instance of same fragment. To avoid multiple instance and restart check this sample app for SingleActivity Architecture with multiple backstack https://github.com/beetlestance/android-extensions/tree/main/navigation