Skip to content Skip to sidebar Skip to footer

Component Navigation , Pop From Backstack With Arguments

Let's say I have three fragments, A, B, C; A -> B <-> C Between B and C it is a circular relationship. Either B or C fragments requires arguments, example val args =

Solution 1:

You can pop fragments from back stack simply by adding a popUpTo attribute to a navigation action. This way you navigate using an action with arguments, but with pop back stack behaviour.

For example, you can add attribute app:popUpTo="@+id/fragmentB" to the action action_fragmentC_to_fragmentB. This way you'll be popping fragmentC from backstack each time you go from fragmentC to fragmentB.

See the docs with example for this here.

There's another option, which is likely an overhead for the case you described, but that allows to use popBackStack method and send arguments - using 'navigate back with result' approach. For it fragments should implement an interface (callback) with a method that receives bundle. Use addOnBackStackChangedListener in the fragment manager to trigger this method, providing all the data necessary, after popBackStack is called. (Described here in the section "How to navigate back with a result?": https://medium.com/google-developer-experts/using-navigation-architecture-component-in-a-large-banking-app-ac84936a42c2, and with slightly different implementation here: https://medium.com/@zawadz88/david-vávra-thank-you-for-this-great-article-ae3e602b880a)

Post a Comment for "Component Navigation , Pop From Backstack With Arguments"