Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android SharedViewModel实例不从原始实例恢复数据_Android_Mvvm_Android Architecture Components_Android Navigation_Android Architecture Navigation - Fatal编程技术网

Android SharedViewModel实例不从原始实例恢复数据

Android SharedViewModel实例不从原始实例恢复数据,android,mvvm,android-architecture-components,android-navigation,android-architecture-navigation,Android,Mvvm,Android Architecture Components,Android Navigation,Android Architecture Navigation,我有一个片段,我在sharedViewModel中更新了一个整数,这就是shopsFragment class ShopFragment : Fragment(), AppBarLayout.OnOffsetChangedListener { private val model: SharedViewModel by viewModels() override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

我有一个片段,我在sharedViewModel中更新了一个整数,这就是shopsFragment

class ShopFragment : Fragment(), AppBarLayout.OnOffsetChangedListener {

    private val model: SharedViewModel by viewModels()

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        model.updateTotal(200)
    }
}
现在,我需要共享数据的另一个片段是
BottomSheetDialogFragment
,在这个片段中,我通过这样做得到了
sharedViewModel
的一个实例

class CartBottomSheet: BottomSheetDialogFragment() {

    private val model: SharedViewModel by viewModels ({requireParentFragment()})

 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
          model.getTotal().observe(viewLifecycleOwner, Observer { total ->
            sheet_total_price.text = "$$total.00"
        })
    }
现在,当我尝试获取我在另一个片段中发布的200时,它显示为0,这意味着该sharedViewModel的实例是一个新实例,因为它返回0,因为我的viewmodel实例用0初始化了一个公共共享总计

class SharedViewModel: ViewModel() {
    private val totalData = MutableLiveData<Int>()
    private var sharedTotal = 0

 fun updateTotal(total:Int){
        sharedTotal = total
        totalData.value = sharedTotal
    }

    fun getTotal():LiveData<Int>{
        return totalData
    }
class-SharedViewModel:ViewModel(){
private val totalData=MutableLiveData()
私有变量sharedTotal=0
fun updateTotal(总计:Int){
sharedTotal=总计
totalData.value=sharedTotal
}
fun getTotal():LiveData{
返回总数据
}
现在,我的问题是,我是否需要将
BottomDialogFragment
这个
sharedViewmodel
实例作为一个包传递给它来使用,或者是否有任何方法可以获得相同的实例来获得total的值


谢谢

您可以为
CartBottomSheet
片段将
ShopFragment
设置为
targetFragment
。这样,当您创建共享虚拟机时,您将得到相同的实例。基本上,如果您将其组合在一起,您可以通过以下代码实现:-

class CartBottomSheet: BottomSheetDialogFragment() {
    private val model: SharedViewModel?=null

    companion object {
        fun show(fragmentManager: FragmentManager, parentFragment: Fragment) {
            val sheet = CartBottomSheet()
            sheet.setTargetFragment(parentFragment, 13)
            sheet.show(fragmentManager, sheet.tag)
        }
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        targetFragment?.let {
            // Create model here with it 
        }
    }

}
所以现在开始的时候你应该打电话

CartBottomSheet.show(fragmentManager!!, this)

由于Shops不是Cart fragment的父级,我认为RequirePreparent是问题所在,我不知道是否有比将viewmodel作为一个包传递给此对话框fragment更干净的方法获取viewmodel。我使用导航组件打开带有val bundle=bundle()bundle.putSerializable(“Cart”,ArrayList(cartItemList))的片段findNavController().navigate(R.id.bottomSheet,bundle)什么是SelectBranchBottomsheet我认为是CartBottomSheet.show(fragmentManager!!,this)模型应该在我现在创建的时候创建?所以现在,CartBottomSheet是ShopFragment的子产品,使用这种方法吗?请看一看,因为您使用的是nav控制器。。