当前位置:网站首页>Jetpack compose management status (I)
Jetpack compose management status (I)
2022-06-21 17:40:00 【Lighthouse @kuaidao】
Preface
Jetpack Compose It can help you clarify the status in Android Storage location and usage in the application , State management It's using Compose Development ui Primary problems .
Jetpack compose Introduce new development paradigm (MVI)
A state in an application is any value that can change over time . It's a very broad definition , from Room Database to class variables , It's all covered .
all Android The application will show the status to the user . Here is Android Some state examples in the application :
- Information prompt control displayed when a network connection cannot be established .
- Blog posts and related comments like background changes , perhaps icon change .
- Ripple animation played when the user clicks the button .
State and combination
because Compose yes declarative Toolset , The interface cannot be manually operated according to the previous command ui Refresh . The only way to update it is to call the same composable item with a new parameter , Here we need to introduce state control . The change of state will trigger the recombination of composable functions .
@Composable
fun HelloContent() {
Column(modifier = Modifier.padding(16.dp)) {
Text(
text = "Hello!",
modifier = Modifier.padding(bottom = 8.dp),
style = MaterialTheme.typography.h5
)
OutlinedTextField(
value = "",
onValueChange = {
},
label = {
Text("Name") }
)
}
}

If you run this code , Will not see any reaction .OutlinedTextField Can't input content , This is because ,OutlinedTextField Will not update itself , But it will be in its value Parameter change When the update , This is because Compose The working principle of combination and reorganization in . The update here is OutlinedTextField You can recombine functions .
Important vocabulary :
The initial combination : Create a composite by running composable items for the first time .
Combine :Jetpack Compose Description of the interface built when executing composable items .
restructuring : In the data ( state ) Rerun composable items when changes occur to update the composition .
States in composable items

1. Composable functions can use remember Composable items remember individual objects , The system will be controlled by remember The calculated values are stored in the combination , And returns the stored value during reorganization .remember It can be used to store variable objects , It can also be used to store immutable objects .
2.mutableStateOf Will create observable MutableState,MutableState Is with the Compose Observable types for runtime integration .
Items in a combination can be declared MutableState Object has three methods :
val mutableState = remember {
mutableStateOf(default) }
var value by remember {
mutableStateOf(default) }
val (value, setValue) = remember {
mutableStateOf(default) }
tips:
although remember Can help you stay in shape after reorganization , But it won't help you in Configuration changes Keep the state after ,
So , You must use rememberSaveable.rememberSaveable It will be saved automatically and can be saved in Bundle Any value in .
For other values , You can pass it in to custom Saver object .
remember Composable items and MutableState Composable item relationship
remember Composable items remain in state in composable functions .MutableState Is to implement the State The observable state of the interface . In writing
remember {
mutableStateOf(default) }
Other supported state types
Jetpack Compose Other observable types are supported , stay Jetpack Compose Before reading other observable types in , You must convert it to State, In order to Jetpack Compose It can automatically reorganize the interface when the state changes .
Compose With some can be based on Android Common observable types used in applications State Function of :
LiveData
Flow
RxJava2
The main points of :Compose Will read State Interface Automatic reorganization interface .
If you are in Compose Use in LiveData Other observable types , It should be converted to State, Then use something like LiveData.observeAsState() Composable extension functions such as read it in composable items .
Be careful : stay Compose The variable object ( Such as ArrayList or mutableListOf()) Using as status will cause users to see incorrect or stale data in your application . Whether it is an observable object depends on whether State Interface , Unobservable objects are not recommended
Stateful and stateless
Use remember Composable items that store objects create internal states , Make the composable item stateful .HelloContent Is an example of stateful composable items , Because it will keep and modify its own name state . There is no need to control the state at the caller , And you can use the state without managing the state by yourself ,“ A stateful ” It will be very useful . however , Composable items with internal states are often not easy to reuse , It's also harder to test .
Stateless composable items refer to composable items that do not maintain any state . A simple way to achieve statelessness is to use [ Status up ].
When developing reusable composable items , You usually want to provide both stateful and stateless versions of the same composable item . Stateful versions are convenient for callers who don't care about state , Stateless versions are necessary for callers who need to control or promote state .
@Composable
fun HelloContent() {
Column(modifier = Modifier.padding(16.dp)) {
var name by remember {
mutableStateOf("") }
if (name.isNotEmpty()) {
Text(
text = "Hello, $name!",
modifier = Modifier.padding(bottom = 8.dp),
style = MaterialTheme.typography.h5
)
}
OutlinedTextField(
value = name,
onValueChange = {
name = it },
label = {
Text("Name") }
)
}
}
The system provides rememberxxx function 
边栏推荐
猜你喜欢

Analysis of 43 cases of MATLAB neural network: Chapter 27 prediction of LVQ Neural Network - face orientation recognition

module. Exports points to problems

常见设置模式

AS 3744.1标准中提及ISO8191测试,两者测试一样吗?

欧洲家具EN 597-1 跟EN 597-2两个阻燃标准一样吗?

加速云原生应用落地,焱融 YRCloudFile 与天翼云完成兼容性认证

拉格朗日插值

【没搞懂路由策略?盘它!】

Common setting modes

稳若磐石的焱融 SaaS 服务平台背后,是数据生态的崛起
随机推荐
PTA L3-031 千手观音 (30 分)
Kotlin DSL build
Why did you win the first Taosi culture award of 20000 RMB if you are neither a top R & D expert nor a sales bull?
shamir
Yrcloudfile of Yanrong technology has completed the compatibility certification with ANTP to jointly create a new blueprint for storage
Nacos注册中心-----从0开始搭建和使用
Software test system learning and construction (13) - basic requirements for test engineers of test foundation
list的使用
Preorder traversal of BM23 binary tree
BM23 二叉树的前序遍历
vector的模拟实现
How to perform en45545 fire test for battery shell
第五章 操作位和位串
Kubernetes + 焱融 SaaS 数据服务平台,个性化需求支持就没输过
Variables and pointers
path.join() 、path.basename() 和 path.extname()
Kotlin annotation declaration and use
ViT杀疯了,10+视觉Transformer模型详解
Software test architecture learning and construction (14) - overview of software test and development model of test foundation
Leetcode 25: a group of K flipped linked lists