Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recreating backstack with Android Navigation Architecture Component

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?

like image 253
limdale Avatar asked Aug 17 '18 14:08

limdale


1 Answers

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, ...)
like image 118
limdale Avatar answered Nov 20 '22 18:11

limdale