I have a fragment A,B,C. Its okay when navigating from A -> B, but from B -> C it crashes.
Here is my Navigation
Here is my navigation code
categoryProductItemListAdapter.setOnItemClickListener { val action = CategoryProductItemsDirections.actionCategoryProductItems2ToProductItem(null, it) navController = Navigation.findNavController(requireView()) navController?.navigateUp() navController?.navigate(action) }
Here is the XML code for the destination to productItem
<fragment android:id="@+id/categoryProductItems2" android:name="com.sample.store.main.dashboard.ui.ui.home.categoryitems.CategoryProductItems" android:label="CategoryProductItems" tools:layout="@layout/fragment_category_product_items"> <argument android:name="category_global" app:argType="com.sample.store.data.globalmodels.response.categories.Category" /> <action android:id="@+id/action_categoryProductItems2_to_productItem" app:destination="@id/productItem" app:enterAnim="@anim/enter_from_right" app:exitAnim="@anim/exit_to_right" app:popEnterAnim="@anim/fragment_open_enter" app:popExitAnim="@anim/fragment_fade_exit" /> </fragment>
And here is the error:
java.lang.IllegalArgumentException: Navigation action/destination com.sample.store.full:id/action_categoryProductItems2_to_productItem cannot be found from the current destination Destination(id/navigation_home) label=Home class=com.sample.store.main.dashboard.ui.ui.home.mainui.HomeFragment
I don't know what happened, but it seems that the navController is looking for the "navigation_home"
This is more a heads up than an answer. But I hope it helps.
Summary: (As others have already said:) Successive calls to Navigation functions are the reason of most of these exceptions.
Given how the android components are structured, specially how MediatorLiveData works, people may sometimes want to join data nodes in a single observable data holder (LiveData).
If an observation of this mediator is linked to dynamic Navigation functions, bugs will undoubtedly appear.
The reason is that sources may change a LiveData value a successive number of times equal to the amount of sources connected to the Mediator.
This is a perfectly good idea, BUT. Repeated changes to the NavController will definitely result in undesirable outcomes.
This may include:
popping the backStack twice.
Going from A -> B twice in a row giving an exception of A not found
the second time.
This is a big testing problem specially since the issue of one Fragment may cascade to the underlaying stacks, and so when an exception of Direction not found may arise in one Fragment, The real culprit may be found on the Fragment on TOP of the one giving the exception.
In reality this would be easily solved by creating a self cancelling thread executor scheduled.cancel(true);
with a delaying tolerance on the mediatorLiveData itself (the onChange to be precise, not the setValue() since eager inner state updates are the entire and only purpose/joke of the mediator IMHO (sorry, no postValue() allowed!)).
Not to mention that the mediator itself is an incomplete component...
Another easier approach is to make sure that onChange calls from a MutableLiveData are performed if and only if !Object::Equals, and prevent repeating calls to onChange() which is still a testament of the incompleteness of the MediatorLiveData/LiveData. (Just be extremely careful with Lists)
At all costs avoid performing successive calls to a NavController, and if you somehow MUST, then a delayed runnable may be your only way to achieve it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With