I am trying to figure out how to skip some fragments in a back stack of navigation component. I have application structure like this
- categories_fragment
- category1_collection_fragment
- category1_collection_item_view_fragment
- category1_collection_item_edit_fragment
- category2_collection_fragment
...etc
The workflow should look like this:
- user enters application - categories_fragment is open by default
- user opens category1_collection_fragment
- user opens category1_collection_item_view_fragment
- user opens category1_collection_item_edit_fragment
- the user edits the item and saves it
- user is presented with category1_collection_item_view_fragment
- user clicks the back button and is taken to step 2 and can repeat 3 - 6 actions
Here's how navigation graph looks like:
<fragment
android:id="@+id/categories_fragment"
android:name="com.domain.CategoriesFragment">
<action
android:id="@+id/action_category1_collection_fragment"
app:destination="@id/category1_collection_fragment" />
</fragment>
<fragment
android:id="@+id/category1_collection_fragment"
android:name="com.domain.category1.Category1CollectioNFragment">
<action
android:id="@+id/action_category_1_view_fragment"
app:destination="@id/category1_view_fragment"/>
</fragment>
<fragment
android:id="@+id/category1_view_fragment"
android:name="com.domain.category1.Category1ViewFragment">
<action
android:id="@+id/action_view_fragment_to_edit_fragment"
app:destination="@id/category1_edit_fragment" />
<argument android:name="item" />
</fragment>
<fragment
android:id="@+id/category1_edit_fragment"
android:name="com.domain.category1.Category1EditFragment">
<action
android:id="@+id/action_edit_fragment_to_view_fragment"
app:destination="@id/category1_view_fragment"
app:popUpTo="@+id/profile_collection_fragment"/>
<argument android:name="item" />
</fragment>
What I have implemented kinda works and the user is taken to category1_collection_fragment after editing and clicking back button, however, I get crash when trying to open another/or same item in the collection. The error is navigation destination is unknown to this NavController.
I checked what navigation controller destinations were changed during this cycle using addOnNavigatedListener:
1. categories_fragment
this one is obvious it's open by default
2. category1_collection_fragment
when user click on the action that takes him to the collection
3. category1_collection_view_fragment
when the user selects an item from the collection and opens it
4. category1_collection_edit_fragment
when the user clicks to edit item
now this next part it vier 3 steps happen at the same time when
navigation controller executes : @id/action_edit_fragment_to_view_fragment
5. category1_collection_view_fragment
6. category1_collection_fragment
7. category1_collection_view_fragment
so the last destination that has been placed it category1_collection_view_fragment and even though I see category1_collection_fragment open which is what I want the destination is
category1_collection_view_fragment from which I am unable to open other items, and when I do I get the crash.
What's wrong with this? How can I make item save open view fragment without history about edit fragment being opened?
I was thinking about just using findNavController().popBackStack() instead of opening view fragment after save, but I need to pass an updated item data, to the view fragment and using this I can't do that.
EDIT
While trying to figure out what's happening I noticed that when toolbar is setup with navigation controller with setUpWithNavController navigate up icon is clicked, afterwards navigation works the way I want, but this not the case with back button, even though back button takes me to the fragment i want I can't navigate from there anywhere else due to the crash
