Ein Compose-Zähler, der nie zählt, und was er über State lehrt
Auf dem Bildschirm wird der Button immer wieder geklickt, doch der Zähler bleibt bei null, selbst nachdem der State veränderbar gemacht wurde. Miriana Itani, Android Team Lead mit mehr als 60 nativen Apps im Play Store, nutzt diesen Fehler, um zu zeigen, wie Jetpack Compose State tatsächlich verfolgt. Für Android-Entwicklerinnen und -Entwickler, die den Abschied von XML erwägen, bleibt die Frage, welche Architektur Compose-Code sauber und testbar hält.
Erfahren Sie, wie remember, Mutable State und State Hoisting zusammenspielen und warum sie MVVM gegenüber MVI bevorzugt
Miriana Itani, Android Team Lead, erklärt, warum Google Jetpack Compose für Android-UIs empfiehlt, und programmiert live einen Zähler, um zu zeigen, wie Mutable State und remember die Recomposition steuern. Sie vergleicht MVI mit MVVM, empfiehlt MVVM mit Compose für saubereren und besser testbaren Code und schließt mit einer Einführung in State Hoisting.
“Compose beobachtet also die Änderungen am State und passt die UI anhand der Änderung dieses State auf Grundlage des Events an.”
“Jeder einzelne Intent wird also mindestens zwei oder drei Klassen erfordern. Wenn wir also eine große Anwendung haben, die, sagen wir, mit 10 Buttons arbeitet, haben wir mindestens rund 30 Klassen.”
What you will learn
Erklären, warum eine einfache Variable in einem Composable bei jedem Klick zurückgesetzt wird und die UI nie aktualisiert.
remember mit mutableStateOf kombinieren, damit Compose den State über Recompositions hinweg behält, wohl wissend, dass er eine Rotation nicht übersteht.
MVI und MVVM mit Jetpack Compose hinsichtlich Klassenanzahl, Lesbarkeit und Testbarkeit vergleichen.
State Hoisting anwenden, um ein Composable in zustandsbehaftete und zustandslose Teile aufzuteilen, die leichter zu testen sind.
Die Compose-Vorschau, Kotlin-Theme-Klassen sowie Column-, Row- und Box-Layouts anstelle von XML-Ressourcen nutzen.
Details
Level
Intermediate
Format
Tech talk
Technical depth
Practitioner
For
Early career, Mid-career
Length
27:25
Published
June 2022
Questions this talk answers
Warum sollten Android-Entwickler auf Jetpack Compose umsteigen?
Google hat es als stabile Version veröffentlicht und empfiehlt es, und es erstellt UIs mit weniger Code über Kotlin-APIs. Composables wie ein gemeinsamer Loader werden einmal geschrieben und wiederverwendet, und Compose zeichnet nur die Teile der UI neu, die sich ändern.
Warum bleibt ein Compose-Zähler bei null, wenn der Button geklickt wird?
Eine einfache Variable wird bei jedem Klick wieder auf null initialisiert, und das Einbetten in Mutable State verfolgt sie nur. Erst remember zusammen mit mutableStateOf lässt Compose den vorherigen State behalten.
Behält remember den State, wenn das Smartphone gedreht wird?
Nein. Eine Konfigurationsänderung wie eine Rotation setzt den gemerkten Wert zurück, und Compose bietet eine separate Funktion, um State über Konfigurationsänderungen hinweg zu sichern, die die Sprecherin erwähnt, aber nicht behandelt.
Sollte man mit Jetpack Compose MVI oder MVVM verwenden?
Die Sprecherin empfiehlt MVVM, weil MVI pro Intent zwei oder drei Klassen benötigt, also rund 30 Klassen für 10 Buttons. Ihrer Ansicht nach ist MVVM einfacher zu schreiben, sauberer und leichter zu testen.
Was ist State Hoisting in Jetpack Compose?
Es teilt ein Composable in eine Funktion, die den State hält, und eine weitere, die die UI enthält und den Wert erhält. So entsteht ein zustandsloses Composable, und das Testen wird einfacher.
So today we are going to be talking about Jetpack Compose. So if you haven't heard about Jetpack Compose before, it is the new way of doing UI developed by Google. So Google has developed Jetpack Compose a couple of years ago. It was in bedtend and it was fully released as a stable version and Google is recommending all Android native developers.
to move into Jetpack Compose. Why should we use Jetpack Compose? So Jetpack Compose simplifies and accelerates the UI, brings the application to life with less codes, powerful tools, and initiative Kotlin APIs. And as we all know also, or if you aren't familiar with this, also Google moved Android from Java to Kotlin. It still supports Java, but at the moment it is more recommended to use Jetpack Compose.
Kotlin because it's a new language. So also Jetpack Compose builds the UI in a way of like objects. So they're like, let's assume we have, for example, a loader. And this loader is a custom loader like Lottie, for example. And this loader is going to be visible in every single page of the application. Rather than having this loader be included in every single page or having to build it dynamically, in a base activity, we build the loader in a base activity. We build the loader as a class using UI and we just call the composable widget. This simplifies libraries, reusability of codes in UI.
And it also saves memory, which we're going to see in a few. We're going to do a live coding session. We're going to see how Jetpack Compose actually helps us in saving memory. How does Compose work? Compose works usually by observing states. If you guys are familiar with NVVM, we used to observe live data. This is the architecture that also Google implemented a few years ago and it's now recommended to be used.
So NVVM is when we save a variable inside the view model and the view communicates with the view model and the view model communicates with the repository and the model, the data. So that's the word of NVVM. So similar to that concept, this is how Jetpack Compose works. So the Compose observes the changes to the state and it modifies the UI based on the change of that state based on the event.
So the event is going to dispatch the update state and the update state is going to change the display in the UI. OK, so now we are going to move to a live coding session. OK, just let me see how to share my screen over there. OK, there we go. I'm also going to. So this is your, this is the composable layout.
As we could see, there is main activity. Oh, sorry. First, if you want to create a composable activity, just go into new. Compose, new empty compose activity. That's it. Or if you want to create a new project, can go new project, empty compose activity. And you're going to be seeing something like this without the commented code that I have already on my screen.
OK, so now we're going to have like this is the greeting. It's Android. But as we could see here, it's hello testing. So why is it hello testing? Because I'm using the preview. The default preview is just a way that Google gave us rather than every time running the application. We just have a sample display of what we're going to see on the application over here.
So I have the word testing. If I change it into test, for example. We're going to automatically change over here into test. This is the default preview. But if I run my application. I'm going to try to run it. Just give me a second. OK. Let's see. I'm not sure you guys can't see my screen. OK, just give me a second to show you my screen.
So now you're able to see. Hello Android. The reason we're seeing Hello Android. rather than test. is because when we are seeing Hello Android. is because when we are calling the function. inside the Jetpack Compose. we are calling the function. we are calling it. greeting. and we're giving it Android. So now you're able to see Hello Android.
The reason we're seeing Hello Android. rather than test. is because when we are calling the function. inside the Jetpack Compose. we are calling it. greeting. and we're giving it Android. Let me try to. OK. So far so good. Now I'm going to split only to the code base. And we are going to implement a simple counter. just to display how the states work using Jetpack Compose.
I already have those codes pre-coded. just to save time. Maybe we could go into MVI after we're done with this. if we had time. So I'm going to comment this default code. and comment this. So what I did over here is just call a water counter. which is a composable function. Ideally this function should be present in a file composable.
on its own. so that for resubility reason. and for this page to be more readable. and we're just going to run it. so we're supposed to see on the screen. just a text. that's going to display. you've had zero glasses. That's exactly what we're going to be seeing. And also it's. I want to point out that here.
in the default preview. we are still seeing hello test. because over here. I did not change it. Okay. let me share again. my screen. Sorry. we're going to have to keep bumping. from. the code to the emulator. Okay. so it's. you've had zero glasses. sorry. we're going to have to keep bumping. from. the code to the emulator.
Okay. so it's. you've had zero glasses. that's basically it. So if you go back to the code. Okay. we are going now to implement. a counter. a plus counter. that's it. so over here. we have. prior count equals to zero. we're just displaying the text. and we have a button. the on click function is count plus plus.
simple enough. okay. so that's all that we're doing over here. and. this should not be any problem. and I'm going to run it. Okay. so now I'm going to be clicking. the add one. so. I'm actually clicking it. as you can see on the screen. but nothing is changing. and I'm going to. paste the code over here.
that we just wrote. I'm going to paste it. and I'm going to tell you. exactly what is wrong. over here. so. what we did is. we actually had. a var count equal to zero. and we're having count plus plus. but the problem with this. is that every time. I am clicking on the button. and the count plus plus.
it is actually. re-initializing. var count equals to zero. so technically speaking. the state is never changing. it's always zero. so. now we're going to see. how we are going to solve this. or attempt to solve it. since we said. we're going to be working. with states. and we said that states resemble. live data. mvvm. so we're going to be.
working with the mutable states. because they're going to be changing. essentially everything. that's a mutable state. is any variable. in the code. that is going to change. over any point in time. think of it as a. display of loader. display loading. success. error. so this is a change. state that has to change. over time. so that's initial it.
I'm just going to import those. And I'm. going to run the app again. and I'm going to paste. the code over here. I just added. remember. just added. the. mutable state of the integer. that's all I did. and now I'm running the code again. and as we can see. nothing's changed. now I'm going to click. on the button.
and still. there is no zeros. the. the counter is not changing. because having the state. as a mutable. does not actually. have composed. dispose the event. it's just tracking it. it's not remembering its state. so what we need to do. is add. remember. I'm going to show you what I did. So this is exactly what I did.
and I'm going to import it. because it's a function. done already by compose. okay. so usually remember. and the mutable states. technically speaking. they're always bundled together. so that the compose. always remembers the previous state. so that it can. dispose. an event. of. the newer state. so they're technically speaking. always together. and now we're going to. rerun the application.
and now I promise you. it's going to work. should work. and as you can see. okay. I said this should work. but it did not work. just give me a second. it did not run well. okay. and now as we can see. oh. you can see. you can see it. it's working. just give me. okay. here it is.
now as you can see. the counter. is working correctly. now I want to mention over here. that the remember. I'm going to post the code. also. so. the last code. that I'm going to be posting. is the correct way. of doing. the mutable states. so. the remember over here. is not going to work. if we have. configuration changes.
so. if we. if I turn the. if I auto rotate my phone. it is going to automatically. come back to zero. there is a function in compose. that's going to save the state. throughout the configuration changes. but we're not going to go through it. but I thought I should mention it. so technically speaking. this is the beauty of state in compose.
compose. so what's happening over here. so. in this function. compose is remembering the state. of the initial value. and is changing it. when there is a need to change. and. the beauty of this. is that. well. now we only have one function. but imagine if we have. we have. um. a button. a list. counter. and a few other things.
so. compose. at the moment. is only going to. re. redisplay the text. it's going to change the UI. only of the text. which is a huge. memory consumption. rather than. redoing the whole UI. over and over again. it's just this. it's just changing the value of the text. text. and this is helping us a lot. and if we have the water.
counter. in a different screen. we would be able to call this. from anywhere inside the application. by simply just adding it over here. and I wanted to note something. that in compose. here I'm using a column. because I want the values. the UI's displayed. vertically. however if I wanted them to be displayed horizontally. I would do is.
a row. and if I wanted them. both horizontally and vertically. something like a constraint layout. I should be using. a box. so. so far. concerning what we've covered. for the time being. does anybody have any questions? I'm sorry. I didn't realize my cat was behind me. okay. okay I guess we can. move on to something that's. something that's.
a bit. a bit more architectural. okay. so. for the next part of our session. I'm going to talk briefly about. compose with. and. Oh, no, this is native Android Kotlin. Not JavaScript. No worries. So for the next part of our session, we are going to be talking briefly about the new architectures that are booming nowadays. So we have Jetpack Compose with MVVM, which is the initial architecture that is recommended by Google. And there is a new architecture that developers are currently working with, and it's called MVI.
So MVI is based on module view intent, and the intent is not the normal intent that we use in Android Studio. It's not going from one activity to another. No, that's not it. The intent is actually the intent of an action. For example, the intent of clicking a button, like we have here in the counter, the intent of showing a loader, and etc. So it's like intentions, if you want to say.
So what happens here in the module view intent is actually the intent and the view are coupled in a way or another together. And any change in the intent would lead to a change in the view. And this happens by the state. So the state, whenever it's changing, is going to change the intent. And the view would be observing the intent. And the intent would automatically change.
Personally speaking, and this is just a personal opinion, I've tried the module view intent and I've tried the MVVM with Jetpack Compose, and I highly recommend Jetpack Compose for a couple of reasons. So, one of them is the model view intent contains so many classes. If you want, I could show you a code with, you know, I'm going to show you one code with the model view intent. Just give me a minute to run the application.
Okay, so this is actually a code that's written using MVI. It's just a simple loader. And I'm just showing a loader based on changes. So, this is Jetpack Compose that's showing a lot of the animation code. Fill maximum width, fill maximum height. I'm giving it a background color. Nothing very fancy over here happening. So, this is the base Compose view model because, as I said, intent kind of use MVVM, but it observes intent rather than observing states.
And we are using flows, if you're familiar with them. And we're using channels. So, we're sending the intent to channels. Okay, let's see. Let me see. Where are we? Okay, over here. So, our splash view model, because what I'm doing over here is that I'm showing, in the splash activity, I'm showing the splash screen, which is a display of the loader, actually.
Okay. So, this is the Composable Splash screen, and I'm telling it that if the intent of the splash view state was a loading display a loader, else just display an image. That's it. And I'm changing the intent manually over here. So, here I'm just telling it what is the state of it. I'm telling it its state. So, we have three states, which is empty, loading, and error.
So, loading is just going to display the loader. Other than that, it's just going to display an image. But we have all of those classes over here, just so that we could do a simple loader. So, every single intent is going to require at least two or three classes. So, if we have a huge application that's going to, let's say, work with 10 buttons, we're going to at least have around 30 classes.
So, this is my personal opinion on MVI. MVVM is easier to write, cleaner code. Like, you could see, you're technically even not really doing MVVM because you are observing the state using live data rather than observing the live data itself. But it's still way cleaner, easier to test. And for those of you who are crazy about unit testing or who do unit testing, which is, by the way, very highly recommended and everybody should be doing it, but it's also better to use MVVM with Jetpack Compose rather than using MVVM with Jetpack Compose.
So, up till now, does anybody have any question about the architecture, like my personal opinion about an architecture? Like, why are we using MVVM with Jetpack Compose rather than MVVM with Jetpack Compose? Okay, I think everything is perfectly clear. Then, let me reopen the application. And we could talk a bit about the libraries, the themes in Jetpack Compose, everything that's actually completely new in this part.
So, now, here, over here, we have the UI theme. So, before, if you're familiar with Android or if you remember, we had this REST folder, the values, and everything actually was here. But now, with Jetpack Compose, everything is in classes, the themes, the type, the type, which is the font actually, the color. So, now, here, everything here is in separate classes, and it's in a package.
This is also easier to maintain rather than having a completely separate package. So, this is also one of the things that are considered a huge change in Jetpack Compose. To add Jetpack Compose to our application, and as I said, it is a stable version, and you should, people should be taking advantage of the Kotlin delegates inside Jetpack Compose.
So, we are having the Compose UI, the Compose Material, and the Tooling Preview, which is needed to just preview whatever we are previewing over here in this part so that we do not test, we do not run the application every time we need to display something over here. So, even over here, as I said, it's showing greeting because that is what I told it to show.
So, if I refresh it, it's going to take a while to refresh. Yeah, of course, you need a very powerful laptop to work with the Compose. Well, to work with Android Studio to begin with, not only Compose. Okay, so, it's rendering the view, and here it is. I'm just going to minimize it. So, this is our typical default preview.
So, I did not need to run the application to see the preview of this, and this actually helps a lot. So, this is why we have that default preview lifecycle. It's because we are working, well, we should be working with MVVM, regardless whether it's MVI or MVVM. We are working with view models, so we need the lifecycle because the view models are lifecycle aware.
Activity Compose, because we are extending a component activity, which is the new activity that uses Jetpack Compose. UI tooling for the modifier, the padding, the box, the column, everything. So, this is, for example, if I want to use a row instead, and we can refresh. So, this is, for example, if I want to use a table, I want to use a table, and I want to use a table, and I want to use a table.
So, this is, for example, if I want to use a table, I want to use a table, and I want to use a table, and I want to use a table, and I want to use a table for it. So, this is, for example, if I want to use a table, and I want to use a table.
So, this is, for example, if I want to use a table, and I want to use a table, and I want to use a table, and I want to use a table. So, this is, for example, if I want to use a table, and I want to use a table, and I want to use a table. So, this is, for example, I want to use a table, and I want to use a table.
Okay, now that we have that covered. So, just let me check if there is anything that I want to... Yeah, so I just want to add about something about the remember, that it is stored in the composition, and kept across recompositions. Okay, so this is something very important, because as I said, upon recomposition, we need the remember state to remember the state.
And that is actually a wrap-up of our sessions. I expected more questions, honestly. But I am going to drop down my LinkedIn. And please feel free to connect. And if you have any questions, anybody, if you would like to continue discussing more Jetpack Compose states, there's also something that's called state hoisting. I could go into it, but I felt like it's an advanced topic.
But for state hoisting, we technically are just separating the views, because if a composable is holding the remember state on its own, along with everything else, it becomes harder to test. Because they're coupled together. So, with state hoisting, what we actually do is we separate the composable function into two functions. One of them is just going to be holding the state, and the other is going to be holding the rest of the UI, which is the text and the button.
And we are going to pass the value of the variable from the first composable function into the second composable function. So, that's how we, that leads to state hoisting, which is a very important topic that I also advise you to read on. So, I'm just going to drop it down over here. State hoisting and Jetpack compose, because it helps in testing and it helps to maintain a stateless composable widget.
And the state full, and the composable widget that has a state, it's called a state full composable widget. Okay. So, anything else? If anyone would like to add anything, feel free to drop it down right now. We still have like around 10 minutes for our session to end. So, anything? Does anybody like to add anything right now?
Okay. I guess we are done for today. I'm so happy that you were part of this session. And as I said, please feel free to connect. Thank you. And as I said, feel free to connect with me on LinkedIn and we could continue this in private. Okay. Have a good day, everybody. Bye-bye.
Transcribed automatically; names checked against the speaker record. Select a time to jump there.
Article based on this talk
Warum State in Jetpack Compose remember braucht und warum MVVM MVI schlägt
Android Team Lead Miriana Itani programmiert live einen Zähler, der nicht zählen will, und erklärt daran State, Recomposition und Architekturentscheidungen in Compose.
Kurz gesagt
Jetpack Compose ist Googles empfohlener Weg, Android-UIs zu bauen, und Miriana Itani argumentiert, dass sich der Umstieg von XML lohnt: weniger Code, wiederverwendbare UI-Komponenten und eine UI, die nur neu zeichnet, was sich ändert. Ihr live programmierter Zähler zeigt den Haken: State in Compose funktioniert nur, wenn er sowohl veränderbar als auch gemerkt ist. Bei der Architektur bevorzugt sie MVVM mit Compose gegenüber MVI, das ihrer Ansicht nach viele Klassen erfordert und schwerer sauber zu halten ist.
Die wichtigsten Erkenntnisse
Erklären, warum eine einfache Variable in einem Composable bei jedem Klick zurückgesetzt wird und die UI nie aktualisiert.
remember mit mutableStateOf kombinieren, damit Compose den State über Recompositions hinweg behält, wohl wissend, dass er eine Rotation nicht übersteht.
MVI und MVVM mit Jetpack Compose hinsichtlich Klassenanzahl, Lesbarkeit und Testbarkeit vergleichen.
State Hoisting anwenden, um ein Composable in zustandsbehaftete und zustandslose Teile aufzuteilen, die leichter zu testen sind.
Die Compose-Vorschau, Kotlin-Theme-Klassen sowie Column-, Row- und Box-Layouts anstelle von XML-Ressourcen nutzen.
Warum Google Android-UIs in Compose sehen will
Itani beginnt mit den Grundlagen. Google hat Jetpack Compose über einige Jahre entwickelt, als stabile Version veröffentlicht und empfiehlt nativen Android-Entwicklern nun den Umstieg. Die Änderung geht mit Googles Wechsel von Java zu Kotlin einher: Java wird weiterhin unterstützt, aber Kotlin ist die empfohlene Sprache.
Ihr erstes Argument ist Wiederverwendung. Ein eigener Loader, etwa eine Lottie-Animation, die auf jeder Seite erscheint, muss nicht mehr jedem Screen hinzugefügt oder dynamisch in einer Base Activity erzeugt werden. Er wird einmal als Composable geschrieben und überall aufgerufen, wo er gebraucht wird. Ihr zweites Argument betrifft das Rendering in Compose. Ähnlich wie beim Beobachten von LiveData in MVVM überwacht Compose den State und reagiert auf Events:
„Compose beobachtet also die Änderungen am State und passt die UI anhand der Änderung dieses State auf Grundlage des Events an.“ (2:15)
Ein Zähler, der nicht zählen will
Der Live-Coding-Teil beginnt in Android Studio mit einer neuen leeren Compose-Activity und der Standardvorschau, die ein Beispiel des Screens rendert, ohne die App zu starten. Itani baut dann einen Wasserzähler: einen Text mit „Sie hatten null Gläser“ und einen Button, dessen Klick-Handler eine Variable hochzählt. Sie klickt, und nichts passiert.
Der Grund, so erklärt sie, ist, dass jeder Klick die Variable wieder auf null initialisiert, sodass sich der State nie wirklich ändert. Den Wert in Mutable State einzubetten, behebt das ebenfalls nicht, denn Compose beobachtet den Wert nun, ohne ihn festzuhalten:
„Er wird nur verfolgt. Sein State wird nicht gemerkt.“ (9:51)
Erst remember löst das Problem. Itani betont, dass remember und Mutable State „immer zusammengehören“, damit Compose den vorherigen State mit dem neuen vergleichen kann. Später ergänzt sie, dass ein gemerkter Wert in der Composition gespeichert und über Recompositions hinweg behalten wird. Auf eine Grenze weist sie hin: remember übersteht keine Konfigurationsänderungen, daher setzt eine Drehung des Smartphones den Zähler auf null zurück. Compose bietet eine separate Funktion, um State über Konfigurationsänderungen hinweg zu sichern, die sie erwähnt, aber nicht vorführt.
Recomposition, Layouts und die Vorschau
Die Bedeutung reicht über einen funktionierenden Zähler hinaus. Ändert sich der Wert, zeichnet Compose nur den Text neu, statt die gesamte UI neu aufzubauen, was Itani als große Speicherersparnis darstellt, sobald ein Screen Buttons, Listen und weitere Elemente enthält. Da der Zähler ein eigenes Composable ist, kann er von jedem Screen aus aufgerufen werden. Sie behandelt auch die Layout-Bausteine: Column für vertikale Anordnung, Row für horizontale und Box dort, wo Entwickler früher zu einem Constraint Layout gegriffen haben.
Auch Themes ändern sich. Farben, Typografie und das Theme selbst liegen nun in Kotlin-Klassen innerhalb eines Packages statt im alten Ordner res/values, was sie leichter wartbar findet. Sie geht die wichtigsten Abhängigkeiten durch (Compose UI, Compose Material, die Tooling-Vorschau, Lifecycle für lifecycle-bewusste ViewModels und Activity Compose für ComponentActivity) und kommt auf die Vorschau als täglichen Zeitsparer zurück:
„Ich musste die Anwendung also nicht starten, um die Vorschau davon zu sehen, und das hilft tatsächlich sehr.“ (22:24)
Sie ergänzt eine ehrliche Warnung: „Ja, natürlich braucht man einen sehr leistungsstarken Laptop, um mit Compose zu arbeiten.“ (21:28)
MVI versus MVVM
Anschließend wendet sich Itani der Architektur zu. In MVI, Model View Intent, ist der Intent nicht Androids Intent zum Wechseln zwischen Activities, sondern die Absicht einer Aktion, etwa das Klicken eines Buttons oder das Anzeigen eines Loaders. View und Intent sind gekoppelt, und Änderungen laufen über State mithilfe von Kotlin Flows und Channels. Ihre Demo ist ein Splash Screen mit drei Zuständen (leer, lädt und Fehler), der einen Loader oder ein Bild zeigt. Der Preis ist aus ihrer Sicht die Anzahl der Klassen:
„Jeder einzelne Intent wird also mindestens zwei oder drei Klassen erfordern. Wenn wir also eine große Anwendung haben, die, sagen wir, mit 10 Buttons arbeitet, haben wir mindestens rund 30 Klassen.“ (18:02)
Sie stellt klar, dass dies ihre persönliche Meinung ist, nachdem sie beide Ansätze ausprobiert hat:
„Das ist also meine persönliche Meinung zu MVI. MVVM ist einfacher zu schreiben, saubererer Code.“ (18:34)
Außerdem argumentiert sie, dass MVVM mit Compose leichter zu testen ist, und appelliert an alle Entwickler, Unit-Tests zu schreiben.
State Hoisting für testbare Composables
Itani schließt mit einem Thema, das sie als fortgeschritten bezeichnet. Ein Composable, das seinen eigenen gemerkten State neben Text und Button hält, ist schwerer zu testen, weil State und UI gekoppelt sind. State Hoisting trennt beides:
„Beim State Hoisting trennen wir also eigentlich die Composable-Funktion in zwei Funktionen auf.“ (25:07)
Eine Funktion hält den State, die andere enthält die UI und erhält den Wert von der ersten. Das Ergebnis ist ein zustandsloses Composable neben einem zustandsbehafteten, was laut ihr sowohl beim Testen als auch bei der Wartung hilft. Sie empfiehlt Entwicklern, sich damit näher zu befassen.
Den Vortrag ansehen
„Why Jetpack Compose?“ von Miriana Itani, 27 Minuten: https://www.womentech.net/video/why-jetpack-compose-miriana-itani
Über die Sprecherin
Miriana Itani ist Android Team Lead in Beirut, Libanon, und Senior Android Developer mit mehr als 60 im Play Store veröffentlichten nativen Anwendungen. Sie hat verschiedene Teams geleitet, wurde als TechWoman Emerging Leader ausgewählt, und ihre Leidenschaft für Robotik führte sie dazu, eine Abschlussarbeit zu schreiben und einen Artikel über eine autonome KI-Drohne zu veröffentlichen.
Fragen, die dieser Vortrag beantwortet
Warum sollten Android-Entwickler auf Jetpack Compose umsteigen?
Warum bleibt ein Compose-Zähler bei null, wenn der Button geklickt wird?
Behält remember den State, wenn das Smartphone gedreht wird?
Sollte man mit Jetpack Compose MVI oder MVVM verwenden?
Was ist State Hoisting in Jetpack Compose?
Mit KI aus dem Vortragstranskript erstellt. Redaktionelle Prüfung ausstehend.
Frameworks and ideas mentioned
MVVM
MVI (model view intent)
State hoisting
Resources mentioned
Jetpack Compose
Android Studio Compose preview
State hoisting in Jetpack Compose (reading topic the speaker recommends)