14

I'm exploring the the 'Navigation Architecture Component' concept which introduced in Google I/O 2018 last month.

Let say I have an activity with a bottom navigation view and a 'fragment' to host all fragments:-

<android.support.constraint.ConstraintLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".view.MainActivity">


        <fragment
            android:id="@+id/root_form"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="androidx.navigation.fragment.NavHostFragment"
            app:navGraph="@navigation/nav_graph"
            app:defaultNavHost="true"
            />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation" />

</android.support.constraint.ConstraintLayout>

And I have 2 actions/tabs in my bottom navigation view which will show FragmentA and FragmentB respectively. FragmentB have a nested (child) fragment FragmentC which can be open from FragmentB.

in my Activity class I link the bottom navigation view with the navigation controller using the 'setupWithNavController()' function. (Kotlin extension function that included in ' androidx.navigation.ui' package) :-

override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)

      bottom_navigation.setupWithNavController(findNavController(R.id.root_form))


 }

Everything works fine as expected.

But incase user navigate to FragmentA while he is in FragmentC and come back agin to same tab then he will only get the FragmentB not the fragmentC. Its a common pattern in bottom navigation when user return to same tab it should show the last child/nested fragment he was viewing.

I know we can handle this if we have our own fragment transaction but How to resolve this issue in 'Navigation Architecture Component' domain?

Niroshan
  • 1,174
  • 1
  • 12
  • 30
  • Did you check this? https://stackoverflow.com/a/50626510/7765139 Right at the end I shared a piece of code that might help you – Levi Moreira Jun 22 '18 at 12:19
  • Thanks for this, but not addressed my issue of restoring the previouly open child/nested tab when come back to the same tab in bottom nevigation. – Niroshan Jun 25 '18 at 06:03
  • 1
    @Niroshan did you get the solution for this issue, i'm looking same thing. – Ramki Anba Aug 29 '18 at 13:51
  • Not found anything yet! I mean any straight forward ways. – Niroshan Sep 03 '18 at 08:53
  • using fragment transaction would be a more realistic way because navigation component might not be that production-ready for now. I solve this issue with show/hide because project can't wait for official solution or codelab example. – Robert Jan 05 '19 at 14:46
  • Any clean solutions, other than using a view pager and than having a nav graph for each bottom nav tab? – MobDev Jun 21 '19 at 22:52

2 Answers2

0

You can use a container fragment which holds the viewpager and is the navigation location from parent. When you your in A or B you can hit C and then when you go back it will go back to parent which will still be at which ever fragment tab you were at.

The nav graph never knows about Frag A or B but it knows about the container.

MobDev
  • 1,219
  • 1
  • 12
  • 25
0

You can slove this issue by updateing the action inside navGraph file.

Just specify popUpTo to the parent fragment (FragmentB) and also set popUpToInclusive to true

<fragment
        android:id="@+id/FragmentB"
        tools:layout="@layout/fragment_b">
        <action
            android:id="@+id/root_form"
            app:destination="@id/FragmentC"
            app:popUpTo="@id/FragmentB"
            app:popUpToInclusive="true" />
    </fragment>
Alaa AbuZarifa
  • 1,171
  • 20
  • 39