0

I am using Navigation Component in my project and :

I need to open a fragment in a different level of hierarchy, so that the back stack is created properly too

in my nav_graph.xml there is a hierarchy like this:

HomeFragment -> CollocationFragment -> ChapterFragment --[selfNavigate]--> ChapterFragment -> PlayerFragment

as you see one of my fragments navigates to itself and send an arguments each time like this:

<fragment
    android:id="@+id/ChapterFragment ">

    <action
        android:id="@+id/action_chapterFragment_self"
        app:destination="@id/ChapterFragment" />
    <argument
        android:name="chapterID"
        app:argType="Long" />

</fragment>

in a nutshell, I need to open PlayerFragment from HomeFragment with an appropriate backstack (the hierarchy mentioned above) .

HomeFragment ---[my back stack]---> PlayerFragment

I know that it seems NavDeepLinkBuilder creates the backstack itself but I have no idea how to create a custom backstack by using Navigation component for my fragments in this case.

imansdn
  • 968
  • 1
  • 7
  • 17

1 Answers1

0

eventually I find a way...it's kind of a workaround but it works like a charm:

so, just call navigate function in your desire order to make the hierarchy backstack:

 fun openPlayerFromHome(){
    
       findNavController.navigate(R.id.HomeFragment)
       findNavController.navigate(R.id.CollocationFragment)
       findNavController.navigate(R.id.ChapterFragment,bundleOf("chapterID",id))
       findNavController.navigate(R.id.PlayerFragment)
    
    }

 
imansdn
  • 968
  • 1
  • 7
  • 17