LiveData : 데이터를 관찰해줄 수 있는 친구.

 

MainActivity.kt

class MainActivity : AppCompatActivity() {

    private var testMutableLiveData = MutableLiveData(0)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        findViewById<Button>(R.id.btnArea).setOnClickListener {
            testMutableLiveData.value = testMutableLiveData.value!!.plus(1)
        }


        testMutableLiveData.observe(this, Observer{
            Log.d("testMutableLiveData",testMutableLiveData.value.toString())
            findViewById<TextView>(R.id.textArea).text = testMutableLiveData.value.toString()
        })
    }
}
 

결과:

 

'Android > LiveData' 카테고리의 다른 글

ViewModel + LiveData + LifeCycleOwner  (0) 2022.11.22
Map/SwitchMap  (0) 2022.11.22
Fragment - LiveData / LifeCycleOwner  (0) 2022.11.22
LiveData와 MutableLiveData  (0) 2022.11.22
ViewModel과 LiveData  (0) 2022.11.22

+ Recent posts