Component Navigation , pop from backstack with arguments

前端 未结 1 902
醉梦人生
醉梦人生 2021-01-18 12:34

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<

相关标签:
1条回答
  • 2021-01-18 12:37

    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)

    0 讨论(0)
提交回复
热议问题