Search code examples
androidkotlinviewmodelmutablelivedata

MutableLiveData ArrayList is empty even after postValue() Kotlin


I am now stuck and currently wondering why my mutable arraylist returns null even if it is being updated with postvalue(). I tried to display it using Toast and it displayed [] which I think is null. It had no space in between so it looked like a box. I did toString() it as well in order to show the text. How would I be able to solve this problem?

Here is my Main Activity:

val list = ArrayList<String>()
list.add("text1")
list.add("text2")
val viewmodel = ViewModelProviders.of(this).get(viewmodel::class.java)
viewmodel.Testlist.postValue(list)

ViewModel:

class viewmodel: ViewModel() {
    val Testlist: MutableLiveData<ArrayList<String>> = MutableLiveData()
    init {
        Testlist.value = arrayListOf()
    }
}

Fragment:

Top area:

activity?.let {
    val viewmodel = ViewModelProviders.of(this).get(viewmodel::class.java)
    observeInput(viewmodel)
}

Bottom area:

private fun observeInput(viewmodel: viewmodel) {
    viewmodel.Testlist.observe(viewLifecycleOwner, Observer {
        it?.let {
            Toast.makeText(context, it.toString(), Toast.LENGTH_LONG).show()
        }
    })
}

Solution

  • You post the value to the LiveData object in the activity's viewmodel, which isn't the same instance as the fragment's viewmodel. Let's take look at the way you instantiate the viewmodel in your fragment:

    activity?.let {
        // activity can be refered by the implicit parameter `it`
        // `this` refers to the current fragment hence it's the owner of the view model
        val viewmodel = ViewModelProviders.of(this).get(viewmodel::class.java)
        observeInput(viewmodel)
    }
    

    To get a viewmodel that is shared between your activity and fragment you have to pass the activity as its owner:

    activity?.let { val viewmodel = ViewModelProviders.of(it).get(viewmodel::class.java) }