LiveData观察者未被调用

时间:2018-02-12 14:03:54

标签: android android-fragments mvvm kotlin android-livedata

我有一个托管片段TabBarActivity的活动EquipmentRecyclerViewFragment。该片段接收LiveData回调,但Activity不接收(在调试模式下使用断点进行校对)。如果我调用ViewModel的initData方法,那么Activity回调会触发,这有什么奇怪之处。以下是上述组成部分的相关部分:

TabBarActivity

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    initVM()
    setContentView(R.layout.activity_nav)
    val equipmentRecyclerViewFragment = EquipmentRecyclerViewFragment()
    supportFragmentManager
            .beginTransaction()
            .replace(R.id.frameLayout, equipmentRecyclerViewFragment, equipmentRecyclerViewFragment.TAG)
            .commit()
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)

}

var eVM : EquipmentViewModel? = null
private fun initVM() {
    eVM = ViewModelProviders.of(this).get(EquipmentViewModel::class.java)
    eVM?.let { lifecycle.addObserver(it) } //Add ViewModel as an observer of this fragment's lifecycle
    eVM?.equipment?.observe(this, loadingObserver)//        eVM?.initData() //TODO: Not calling this causes Activity to never receive the observed ∆
}
val loadingObserver = Observer<List<Gun>> { equipment ->
    ...}

EquipmentRecyclerViewFragment

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    columnCount = 2
    initVM()
}

//MARK: ViewModel Methods
var eVM : EquipmentViewModel? = null
private fun initVM() {
    eVM = ViewModelProviders.of(this).get(EquipmentViewModel::class.java)
    eVM?.let { lifecycle.addObserver(it) } //Add ViewModel as an observer of this fragment's lifecycle
    eVM?.equipment?.observe(this, equipmentObserver)
    eVM?.initData()
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val view = inflater.inflate(R.layout.fragment_equipment_list, container, false)
    if (view is RecyclerView) { // Set the adapter
        val context = view.getContext()
        view.layoutManager = GridLayoutManager(context, columnCount)
        view.adapter = adapter
    }
    return view
}

EquipmentViewModel

class EquipmentViewModel(application: Application) : AndroidViewModel(application), LifecycleObserver {
var equipment = MutableLiveData<List<Gun>>()
var isLoading = MutableLiveData<Boolean>()

fun initData() {
    isLoading.setValue(true)
    thread { Thread.sleep(5000) //Simulates async network call
        var gunList = ArrayList<Gun>()
        for (i in 0..100){
            gunList.add(Gun("Gun "+i.toString()))
        }
        equipment.postValue(gunList)
        isLoading.postValue(false)
    }
}

最终目标是让活动只是观察isLoading MutableLiveData布尔值,但由于那不起作用,我改变了活动,只观察设备LiveData以最小化播放中的变量数量。

3 个答案:

答案 0 :(得分:9)

要获得与您的活动相同的ViewModel参考,您需要传递相同的活动,您应该使用ViewModelProviders.of(getActivity),当您通过this时,您会收到ViewModel的新实例。

了解更多信息Share data between fragments

答案 1 :(得分:1)

我将这段代码放在onActivityCreated片段中,请不要小看getActivity;)

if (activity != null) {            
     globalViewModel = ViewModelProvider(activity!!).get(GlobalViewModel::class.java)
    }


globalViewModel.onStop.observe(viewLifecycleOwner, Observer { status ->
            Log.d("Parent Viewmodel", status.toString())
        })

这段代码可帮助我侦听片段中Parent ViewModel的更改。

答案 2 :(得分:0)

Kotlin 答案

<块引用>

如果您正在使用,请删除函数中的这两点:

  1. = viewModelScope.launch { }
  2. 暂停