Android UI Development with Jetpack Compose by Thomas Künneth
Author:Thomas Künneth [Thomas Künneth]
Language: eng
Format: epub, pdf
Publisher: Packt Publishing
Published: 2022-02-21T00:00:00+00:00
Surviving configuration changes
Please recall that our definition of state as data that may change over time is quite broad. For example, we do not specify where the data is stored. If it resides in a database, a file, or some backend in the cloud, the app should include a dedicated persistence layer. However, until Google introduced the Android Architecture Components back in 2017, there had been practically no guidance for developers on how to structure their apps. Consequently, persistence code, UI logic, and domain logic were often crammed into one activity. Such code was difficult to maintain and often prone to errors. To make matters a little more complicated, there are situations when an activity is destroyed and recreated shortly after. For example, this happens when a user rotates a device. Certainly, data should then be remembered.
The Activity class has a few methods to handle this. For example, onSaveInstanceState() is invoked when the activity is (temporarily) destroyed. Its counterpart onRestoreInstanceState() method is called only when such an instance state has been saved before. Both methods receive an instance of Bundle, which has getters and setters for various data types. However, the concept of instance state has been designed for the traditional view system. Most activities held references to UI elements and therefore could be accessed easily inside onSaveInstanceState() and onRestoreInstanceState().
Composables, on the other hand, are usually implemented as top-level functions. So, how can their state be set or queried from inside an activity? To temporarily save state in a Compose app, you can use rememberSaveable {}. This composable function remembers the value produced by a factory function. It behaves similarly to remember {}. The stored value will survive the activity or process recreation. Internally, the savedInstanceState mechanism is used. The sample ViewModelDemo app shows how to use rememberSaveable {}. Here's what the main activity looks like:
class ViewModelDemoActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ViewModelDemo()
}
}
}
We don't need to override onSaveInstanceState() to temporarily save our state used with composables:
@Composable
@Preview
fun ViewModelDemo() {
...
val state1 = remember {
mutableStateOf("Hello #1")
}
val state2 = rememberSaveable {
mutableStateOf("Hello #2")
}
...
state3.value?.let {
Column(modifier = Modifier.fillMaxWidth()) {
MyTextField(state1) { state1.value = it }
MyTextField(state2) { state2.value = it }
...
}
}
}
The app shows three text input fields that receive their values from states assigned to state1, state2, and state3. For now, we will focus on the first two. state3 will be the subject of the Using ViewModel section. state1 invokes remember {}, whereas state2 uses rememberSaveable {}. If you ran ViewModelDemo, changed the content of the text input fields, and rotated the device, the first one would be reset to the original text, whereas the second one would keep your changes.
MyTextField is a very simple composable. It looks like this:
@Composable
fun MyTextField(
value: State<String?>,
onValueChange: (String) -> Unit
) {
value.value?.let {
TextField(
value = it,
onValueChange = onValueChange,
modifier = Modifier.fillMaxWidth()
)
}
}
Have you noticed that value is of State<String?>? Why would I need a value holder whose value can be null, and therefore need to check with value.
Download
Android UI Development with Jetpack Compose by Thomas Künneth.pdf
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Coding Theory | Localization |
Logic | Object-Oriented Design |
Performance Optimization | Quality Control |
Reengineering | Robohelp |
Software Development | Software Reuse |
Structured Design | Testing |
Tools | UML |
Deep Learning with Python by François Chollet(12526)
Hello! Python by Anthony Briggs(9871)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9761)
The Mikado Method by Ola Ellnestam Daniel Brolund(9752)
Dependency Injection in .NET by Mark Seemann(9297)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8262)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7745)
Grails in Action by Glen Smith Peter Ledbrook(7671)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7521)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(6759)
Microservices with Go by Alexander Shuiskov(6526)
Practical Design Patterns for Java Developers by Miroslav Wengner(6422)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6401)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6383)
Angular Projects - Third Edition by Aristeidis Bampakos(5785)
The Art of Crafting User Stories by The Art of Crafting User Stories(5313)
NetSuite for Consultants - Second Edition by Peter Ries(5254)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5075)
Kotlin in Action by Dmitry Jemerov(5023)
