MutableStateFlow StateFlow
唯讀 與 可存取
StateFlow是唯讀,唯讀代表不可以修改,只能讀取。
MutableStateFlow是可讀可寫,可以存取。
MutableStateFlow僅能在 ViewModel 中修改變數。
StateFlow提供給Activity來讀取ViewModel的MutableStateFlow變數。
MutableStateFlow本身就有emit()發射的功能,不需要自己寫。
所以當MutableStateFlow(可讀可寫)變數有任何修改,就自動發射emit()。
Activity,使用StateFlow(唯讀)的collect(),監控是否有變更,有變更就要接收變更的值。
MutableStateFlow.value
value代表變數值,本身就有setValue()與getValue()的功能。
修改
MutableStateFlow變數.value = MutableStateFlow變數.value + 1
_number.value++
ViewModel與MutableStateFlow
MutableStateFlow的變數都是以底線_開頭。
變數不是null
val _變數 = MutableStateFlow<變數類型>(初始值)
val _number = MutableStateFlow<Int>(0)
變數可以是null
val _變數 = MutableStateFlow<變數類型>(初始值)
val _str = MutableStateFlow<String?>(null)
ViewModel與StateFlow
StateFlow的變數不是底線_開頭。
變數不是null
val 變數: StateFlow<變數類型> = _MutableStateFlow變數
val number: StateFlow<Int> = _number
完整程式碼
NumberViewModel
1
2
3
4
5
6
7
8
9
10
11
import androidx.lifecycle.ViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
class NumberViewModel : ViewModel() {
val _number = MutableStateFlow<Int>(0)
val number: StateFlow<Int> = _number
fun increase() {
_number.value++
}
}
MainActivity08是使用StateFlow來做collect()監控並接收變動的資料。
lifecycleScope是Activity的協程,生命周期與Activity一樣,Activity Destoryed的時候,協程占用的記憶體也會被記憶體回收,不會有殘留的記憶體沒被清除。
collect()監控並接收變更資料,是suspend()暫停函式,必須在協程的區域中才能被呼叫。
value是變更的資料。
MainActivity08
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class MainActivity08 : AppCompatActivity() {
private val viewModel by viewModels<NumberViewModel>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val submit = findViewById<Button>(R.id.button)
val textv = findViewById<TextView>(R.id.textView)
lifecycleScope.launch {
viewModel.number.collect { value ->
textv.text = value.toString()
}
}
submit.setOnClickListener {
viewModel.increase()
}
}
}