一对一直播系统开发Jetpact Compose状态管理存储状态的方式
一对一直播系统开发Jetpact Compose状态管理存储状态的方式
以下三种方式,可以实现对非 Bundle 的数据的保存(配置更改后的保存)
Parcelize
代码示例:
@Parcelize data class City(val name: String, val country: String) : Parcelable @Composable fun CityScreen() { var selectedCity = rememberSaveable { mutableStateOf(City("Madrid", "Spain")) } }
MapSaver
data class City(val name: String, val country: String) val CitySaver = run { val nameKey = "Name" val countryKey = "Country" mapSaver( save = { mapOf(nameKey to it.name, countryKey to it.country) }, restore = { City(it[nameKey] as String, it[countryKey] as String) } ) } @Composable fun CityScreen() { var selectedCity = rememberSaveable(stateSaver = CitySaver) { mutableStateOf(City("Madrid", "Spain")) } }
ListSaver
data class City(val name: String, val country: String) val CitySaver = listSaver<City, Any>( save = { listOf(it.name, it.country) },//数组中保存的值和City中的属性是顺序对应的 restore = { City(it[0] as String, it[1] as String) } ) @Composable fun CityScreen() { var selectedCity = rememberSaveable(stateSaver = CitySaver) { mutableStateOf(City("Madrid", "Spain")) } }
以上就是一对一直播系统开发Jetpact Compose状态管理存储状态的方式, 更多内容欢迎关注之后的文章