Encapsulate LiveData

Create internal and external version of the variables that need to be passed out of our class, so that other classes cannot modify the internal data.

import ...

class MyViewModel : ViewModel() {

    // The variable to be passed out of the class
    private val _variable = MutableLiveData<String>() // Private internal class, mutable
    val variable: LiveData<String> // Public external class, immutable
    get() = _variable // With a custom setter to provide the value from the internal class

    init {
        _variable.value = "" // Initialise the internal version of the variable if needed. All uses of the variable in this class should use the underscore version i.e. _variable
    }
}