当前位置:网站首页>Kotlin Compose 状态恢复 rememberSaveable 与 remember
Kotlin Compose 状态恢复 rememberSaveable 与 remember
2022-06-26 06:36:00 【安果移不动】
rememberSaveable 可以保存数据状态。让在旋转屏幕的时候数据仍旧不发生改变
首先实现Parcelable接口
Kotlin 快速实现 Parcelable 接口_安果移不动的博客-CSDN博客
代码
package com.anguomob.jecpack.activity.compose.parcelable
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.MaterialTheme.colors
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.anguomob.jecpack.bean.City
@Composable
fun CityScreen() {
val (city, setCity) = rememberSaveable {
mutableStateOf(City("北京1", "中国1"))
}
Row(modifier = Modifier.padding(10.dp), verticalAlignment = Alignment.CenterVertically) {
TextButton(onClick = {
setCity(City("北京", "中国"))
}, colors = ButtonDefaults.buttonColors()) {
Text(text = "点击修改")
}
Text(text = "${city.name} ${city.country}")
}
}首先修改数据
然后旋转屏幕
你会发现数据没有发生变化

如果使用了
remember{}
点击修改数据 然后旋转屏幕
数据会变成修改之前的数值,。
边栏推荐
- Self attention and multi head self attention (MSA) in transformer
- vs code 使用 prettier 格式化 js 的时候, 函数定义的名称和括号之间有一个空格, 而 eslit 又不允许这个空格.
- [digital signal processing] basic sequence (basic sequence lists | unit pulse sequence | unit pulse function | discrete unit pulse function | difference between unit pulse function and discrete unit p
- Five solutions across domains
- Unsatisfied dependency expressed through field ‘baseMapper‘; nested exceptio
- MySQL 数据库的小白安装与登录
- typescript的type
- Mysql delete in 不走索引的
- Customer Stories | Netease spring breeze: the "spring breeze" of the fun industry, reaching out to all areas through in-depth interaction
- Typescript type
猜你喜欢

Reasons why MySQL indexes are not effective

Temperature alarm

【golang】time相关

C nuget offline cache package installation

Message queue - function, performance, operation and maintenance comparison

Zotero文献管理工具之Jasminum(茉莉花)插件
![[digital signal processing] basic sequence (unit step sequence | relationship between unit step sequence and unit pulse sequence | rectangular sequence | relationship between rectangular sequence and](/img/0c/3aeb7f93576181bbc46d2d1a1aa0e7.jpg)
[digital signal processing] basic sequence (unit step sequence | relationship between unit step sequence and unit pulse sequence | rectangular sequence | relationship between rectangular sequence and

I use flask to write the website "II"

Library management system

MySQL 索引底层原理
随机推荐
GoF23—建造者模式
01 golang and matlab code of knapsack problem
The sysdig 2022 cloud native security and usage report found that more than 75% of the running containers have serious vulnerabilities
浏览器的四大内核:Trident,Gecko,Webkit,Blink
Installing rainbow in various kubernetes with Helm
Decision tree learning notes
China micro cultivator market trend report, technical dynamic innovation and market forecast
Customer Stories | Netease spring breeze: the "spring breeze" of the fun industry, reaching out to all areas through in-depth interaction
Import export simple
DPDK——TCP/UDP协议栈服务端实现(一)
Go语言学习笔记 1.1
View analysis
Phantom star VR equipment product details II: dark battlefield
Installation and login of MySQL database
Unsatisfied dependency expressed through field ‘baseMapper‘; nested exceptio
Thinking skills of technical leaders
GoF23—原型模式
同步通信和异步通信的区别以及优缺点
Type de Typescript
STM 32 uses cube to generate Tim to trigger ADC and transmit through DMA