Get data back from a fragment dialog - best practices?

前端 未结 4 1522
面向向阳花
面向向阳花 2020-12-13 11:03

I\'m converting some of my project to use fragments. How do we communicate with a fragment dialog? I want to create a fragment dialog just to get some text input from the us

4条回答
  •  执笔经年
    2020-12-13 11:33

    There is a new pattern possible which is to share a ViewModel instance between fragments. When instantiating a ViewModelFactory where to get your ViewModels, you have to specify a context as parameter. If the context is the same for both fragments (i.e: the parent activity or parent fragment) and you instantiate the same ViewModel from both fragments, you will get the same instance. This opens a new range of possibilities but also challenges.

提交回复
热议问题