How to pass data from a fragment to a dialogFragment

后端 未结 4 2179
甜味超标
甜味超标 2020-12-05 07:06

I know that this was asked before, but I dont quite understand how to implement it. I have a fragment \"myFragment\" in which I create an object of a \"myDialogueFragment\".

4条回答
  •  执念已碎
    2020-12-05 07:57

    Two Fragments should never communicate directly.

    The recommended way to communicate between fragments is to create a shared ViewModel object. Both fragments can access the ViewModel through their containing Activity. The Fragments can update data within the ViewModel and if the data is exposed using LiveData the new state will be pushed to the other fragment as long as it is observing the LiveData from the ViewModel.

    If you are unable to use a shared ViewModel to communicate between your Fragments you can manually implement a communication flow using interfaces. However this ends up being more work to implement and it is not easily reusable in other Fragments.

    for more information

提交回复
热议问题