ViewModel for Fragment instead accessing Activity ViewModel?

后端 未结 4 1979
醉话见心
醉话见心 2021-01-30 05:35

The problem is quite straightforward. The question is in context of using ViewModels, LiveData and other related Lifecycle aware arch approaches.
I have an Activity with Na

4条回答
  •  悲&欢浪女
    2021-01-30 06:28

    To solve the problem of FirstFragment sharing its view model with its child fragments, you can use this code to access the FirstFragmentViewModel from any of the child fragments:

        // in ChildFragment1
        val firstFragmentViewModel: FirstFragmentViewModel by viewModels(
            { requireParentFragment() }
        )
    
    

提交回复
热议问题