Search code examples
kotlinmvvmviewmodel

How does ViewModel in Android Studio handle configuration change behind the scenes?


I have been playing around with viewModels, however what puzzles me is how ViewModels handle configuration changes behind the scenes, here is an example

Viewmodel

class InitiateviewModels: ViewModel() {
    var list:MutableLiveData<MutableList<Int>> = MutableLiveData()

    fun fillingList(){
        var Integerlist = mutableListOf<Int>(1,2,3,4,5)
        list.setValue(Integerlist)
    }
}

In the ViewModel class, I append the list property with a new Integer list whenever fillingList() method gets called.

MainActivity

class MainActivity : AppCompatActivity() {
    lateinit var Initiation:InitiateviewModels
    lateinit var text:TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Initiation = ViewModelProvider(this)[InitiateviewModels::class.java]
        text = findViewById(R.id.textView1)
        Initiation.fillingList()

        Initiation.list.observe(this, Observer{
            text.text = it.toString()
            Log.d("Answers", "${it?.size}")
            Log.d("Answers", "From Viewmodel ${Initiation.list.value?.size}")

        })
    }
}

In the MainActivity,we call the Initiation.fillingList(), to append the list of integers Integerlist to the list property in the ViewModel, we attach observers to the list property , we Log the size of the list and show the list on the textview

Size of list after a configuration change, both from Observers and directly from the viewmodel property

How the UI looks

Each time the configuration changes, the OnCreate functions stops and gets restarted, meaning OnCreate gets called again, if OnCreate is called again, wouldn't it call Initiation.fillingList() again meaning each screen rotation would increase the list property size by 5?

So when first loading the app the size of the list property would be 5, then 10, 15,20,etc everytime the screen is rotated. What could possibly be the reason for the list not becoming bigger and bigger everytime the Initiation.fillingList() is called?

According to the docs This caching means that you don’t have to fetch data again through common configuration changes, such as a screen rotation., how does this work in this case scenario?

I was just playing around, testing concepts out.


Solution

  • Look at this code of yours more closely:

    fun fillingList(){
        var Integerlist = mutableListOf<Int>(1,2,3,4,5)
        list.setValue(Integerlist)
    }
    

    You are not adding to or enlarging an existing list. You are replacing the LiveData's list with a new one each time this is called, so when you inspect the LiveData contents it is always the same size.

    By the way, variables and property names should not start with a capital letter--that is very confusing to read because that kind of naming by convention would me you're accessing a static object rather than an instance of a class.