0:0027:25

Why Jetpack Compose?

Miriana Itani 27:25 2022

A Compose counter that never counts, and what it teaches about state

On screen, the button gets clicked again and again, yet the counter stays at zero, even after the state is made mutable. Miriana Itani, an Android team lead with more than 60 native apps on the Play Store, uses that failure to show how Jetpack Compose really tracks state. For Android developers weighing the move off XML, the open question is which architecture keeps Compose code clean and testable.

Learn how remember, mutable state and state hoisting fit together, and why she prefers MVVM to MVI

Watch on YouTube

In short

Miriana Itani, an Android Team Lead, explains why Google recommends Jetpack Compose for Android UI and live-codes a counter to show how mutable state and remember drive recomposition. She compares MVI with MVVM, recommends MVVM with Compose for cleaner and more testable code, and closes with an introduction to state hoisting.

“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, 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.”

What you will learn

  • Explain why a plain variable inside a composable resets on every click and never updates the UI.
  • Combine remember with mutableStateOf so Compose keeps state across recompositions, knowing it does not survive rotation.
  • Compare MVI and MVVM with Jetpack Compose on class count, readability and testability.
  • Apply state hoisting to split a composable into stateful and stateless parts that are easier to test.
  • Use the Compose preview, Kotlin theme classes and Column, Row and Box layouts in place of XML resources.

Key takeaways from the speaker

  • android
  • native
  • kotlin
  • jetpack compose

Details

Level
Intermediate
Format
Tech talk
Technical depth
Practitioner
For
Early career, Mid-career
Length
27:25
Published
June 2022

Questions this talk answers

Why should Android developers move to Jetpack Compose?

Google has released it as stable and recommends it, and it builds UI with less code through Kotlin APIs. Composables such as a shared loader are written once and reused, and Compose redraws only the parts of the UI that change.

Why does a Compose counter stay at zero when the button is clicked?

A plain variable is re-initialised to zero on every click, and wrapping it in mutable state only tracks it. Adding remember together with mutableStateOf lets Compose keep the previous state.

Does remember keep state when the phone is rotated?

No. A configuration change such as rotation resets the remembered value, and Compose has a separate function for saving state across configuration changes, which the speaker mentions but does not cover.

Should you use MVI or MVVM with Jetpack Compose?

The speaker recommends MVVM because MVI needs two or three classes per intent, around 30 classes for 10 buttons. In her view MVVM is easier to write, cleaner and easier to test.

What is state hoisting in Jetpack Compose?

It splits a composable into one function that holds the state and another that holds the UI and receives the value. This keeps a stateless composable and makes testing easier.

About this talk

Google has released a new way of dealing with UI. In this workshop, participants will experience a hands-on experience with jetpack compose. Also, they will be introduced to the importance of making the switch from XML to this module. In addition to that, a discussion will occur on MVI vs MVVM which are the latest design architectures currently on the rise.

Topics

Related talks

Browse the library