WAI
Status Update
Comments
il...@google.com <il...@google.com> #2
setupActionBarWithNavController uses the current destination ID to determine if the Up button is shown, so the behavior you're seeing is working as intended.
You can have multiple destinations that use the same Fragment class, so just create a separate destination for recursive calls:
<navigation xmlns:android="http://schemas.android.com/apk/res/android "
xmlns:app="http://schemas.android.com/apk/res-auto "
xmlns:tools="http://schemas.android.com/tools "
android:id="@+id/catalog_nav_graph"
app:startDestination="@id/categories">
<fragment
android:id="@+id/categories"
android:name="somepackage.categories.CategoryListFragment"
tools:layout="@layout/catalog_category_list_frag">
<action
android:id="@+id/action_category_to_category"
app:destination="@id/categoriesRecursive" />
<action
android:id="@+id/action_category_to_product_list"
app:destination="@id/products_frag" />
<argument
android:name="categoryId"
app:argType="integer"
android:defaultValue="0" />
</fragment>
<fragment
android:id="@+id/categoriesRecursive"
android:name="somepackage.categories.CategoryListFragment"
tools:layout="@layout/catalog_category_list_frag">
<action
android:id="@+id/action_category_to_category"
app:destination="@id/categoriesRecursive" />
<action
android:id="@+id/action_category_to_product_list"
app:destination="@id/products_frag" />
<argument
android:name="categoryId"
app:argType="integer"
android:defaultValue="0" />
</fragment>
....
</navigation>
You can have multiple destinations that use the same Fragment class, so just create a separate destination for recursive calls:
<navigation xmlns:android="
xmlns:app="
xmlns:tools="
android:id="@+id/catalog_nav_graph"
app:startDestination="@id/categories">
<fragment
android:id="@+id/categories"
android:name="somepackage.categories.CategoryListFragment"
tools:layout="@layout/catalog_category_list_frag">
<action
android:id="@+id/action_category_to_category"
app:destination="@id/categoriesRecursive" />
<action
android:id="@+id/action_category_to_product_list"
app:destination="@id/products_frag" />
<argument
android:name="categoryId"
app:argType="integer"
android:defaultValue="0" />
</fragment>
<fragment
android:id="@+id/categoriesRecursive"
android:name="somepackage.categories.CategoryListFragment"
tools:layout="@layout/catalog_category_list_frag">
<action
android:id="@+id/action_category_to_category"
app:destination="@id/categoriesRecursive" />
<action
android:id="@+id/action_category_to_product_list"
app:destination="@id/products_frag" />
<argument
android:name="categoryId"
app:argType="integer"
android:defaultValue="0" />
</fragment>
....
</navigation>
vl...@gmail.com <vl...@gmail.com> #3
This is funny, but this workaround exactly i invented. Even destinations ids.
I just thought it isn't right, tho. Alright then.
I just thought it isn't right, tho. Alright then.
Description
Version used:1.0.0-alpha06
Devices/Android versions reproduced on:Android 8.0.0
At first lets assume we have CategoryListFragment. This fragment show categories as simple rows in recycleview.
When we click at any category we go to subcategories screen represented by absolutely same CategoryListFragment class so we can have categories as tree structure.
To achieve that in nav_graph.xml we define something like this:
<navigation xmlns:android="
xmlns:app="
xmlns:tools="
android:id="@+id/catalog_nav_graph"
app:startDestination="@id/categoriesRecursive">
<fragment
android:id="@+id/categoriesRecursive"
android:name="somepackage.categories.CategoryListFragment"
tools:layout="@layout/catalog_category_list_frag">
<action
android:id="@+id/action_category_to_category"
app:destination="@id/categoriesRecursive" />
<action
android:id="@+id/action_category_to_product_list"
app:destination="@id/products_frag" />
<argument
android:name="categoryId"
app:argType="integer"
android:defaultValue="0" />
</fragment>
....
</navigation>
Note: categoriesRecursive is startDestination for the graph. This is important.
Alright, then in Activity we setup Nav with Action bar with NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout)
What do we see:
1. We go at the start destination CategoryListFragment. We see the hamburger menu in action bar and its intended.
2. We click at any item in Category list and navigate to another CategoryListFragment
3. New fragment opens on top of stack and its fine
4. The humburger menu haven't changed into back button. When you click on it - it opens drawer
5. But smartphone's back button works like intended, it returns us to previous CategoryListFragment
Issue only with back button.