Recreating backstack with Android Navigation Architecture Component

假装没事ソ 提交于 2020-08-22 05:09:28

问题


I am trying to implement navigation to specific Detail pages of my app using PendingIntent from a notification, however I am having problems recreating the backstack from the Detail page all the way back to the start destination.

I made a sample app here with a single activity and three fragments to demo this:

Fragment 1   ->   Fragment 2  ->  Fragment 3
(start dest) <-               <-

From Fragment 1 (the start destination), I navigate directly to Fragment 3 using

    Navigation.findNavController(this, R.id.nav_host_fragment).navigate(R.id.fragment2,
            null,
            NavOptions.Builder()
                    .build())

From Fragment 3, when I call Navigation.findNavController(this,R.id.nav_host_fragment).navigateUp() I am navigated back to Fragment 1. Is there a way to get this to navigate to a newly created Fragment 2 instead?


回答1:


Thanks to M.G for pointing to the right direction, I have managed to solve this by manually creating the backstack using the navigation library. This means sequentially calling findNavController(...).navigate(...) multiple times to create a backstack.

For example when I deep link to fragment 3 but want an up navigation back to fragments 1 and 2, I call:

findNavController.navigate(R.id.fragment1, ...)
findNavController.navigate(R.id.fragment2, ...)
findNavController.navigate(R.id.fragment3, ...)


来源:https://stackoverflow.com/questions/51897564/recreating-backstack-with-android-navigation-architecture-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!