Awesome
Jetpack Compose Frequently Asked Questions
Knowledge base about glorious new android ui framework in the form of simple FAQ
π€ What is Jetpack Compose?
βοΈ Jetpack Compose is an Androidβs modern toolkit for building native UI rebuilt mostly from scratch by Google. You can find docs and more info about it in the official docs
π€ How it differs from Anko/Anvil/Splitties-View-DSL or other kotlin UI dsl's?
βοΈ While aforementioned libraries are built on top of ordinary Android Views, whose history dates back to the very first Android release, Jetpack Compose uses just canvas and text tools to draw your widgets.
π€ So, do I have to rewrite my entire app from scratch?
βοΈ Of course, not! In fact, there are some Android that would stay as is, such as WebView, MapView, etc. This is possible because of Jetpack Compose interop tools, such as setViewContent
and ComposeView
which could be used to juggle between both ui frameworks. Also, check out @GenerateView
annotation - this would help you to create a plain old Android View out of your composable
π€ How it differs from React?
βοΈ From user perspective - not so much. Composable functions are roughly equivalent of React functional components, and effects are similar to React hooks. For more details see this talk by Leland Richardson React, meet Compose
π€ Why all composables return Unit instead of some structure, like VDOM?
βοΈ Jetpack Compose uses compiler plugin to inject diffing at compile time. This allows to skip the step of creating a virtual DOM and diffing it with the previous one (process named reconcilation) with more efficient recomposition. For more info, please refer to this video or this blogpost.
π€ Why do I need to write +
to load something or get a theme value?
βοΈ +
is a syntax for executing the Effect, which is basically a memoized computation. You can see more of that in this video or in this blogpost. Note that +
syntax is considered ugly and is a subject to change.
π€ When JC will be ready for production?
βοΈ "When itβs ready". We, as users, hope that playable alpha/beta will be announced on Google I/O 2020, but this is a completely fan-fiction story.
π€ Will JC support other platforms besides Android?
βοΈ Jetpack Compose was built in a way that permits core code reuse between platforms. Combined with ability of Kotlin to compile to other targets makes it possible to build a crossplatform framework on top of compose core, but right now the priority is to deliver best possible experience for Android developers first.
π€ Will JC support animations?
βοΈ Yes, check out :ui:ui-animation
and related projects (like :ui:ui-animation:integration-tests:ui-animation-demos
) in jetpack compose source repo for more examples.
π€ How will dialog and popup window work?
βοΈ Just like other composables, all dialogs would have its own composable duals, but they would do clever stuff with WindowManager under the hood.
π€ Does Jetpack Compose support Android Studio Layout Preview?
βοΈ Currently it does not support Layout Preview. But you can use @Preview
annotation on your composable function without parameters to get a basic live preview of your composable to see it in action.
π€ Does JC work with Constraint Layout?
βοΈ Currently it does not support Constraint Layout. But it will change in the future.
π€ How do I make list widget, like RecyclerView?
βοΈ Compose doesn't have one that is as effective as RecyclerView can be, but for toy projects you can use just a VerticalScroller
with a Column
inside it.
π€ How do I access android.content.Context
in composables?
βοΈ You can get it from ambient like this val context = +ambient(ContextAmbient)
π€ How do I access vector assets in composables?
βοΈ Vector assets can be acquired with val vector = +vectorResource(vectorResourceId)
π€ How do I manage component state?
β Although it is strongly recommended to design your composable is such way that they are pure and depend only on their arguments, sometimes you need to have state somewhere.
This can be achieved by using +state(initial)
effect. This will give you a nice place to store and update your immutable state.
π€ What does @Model
annotation do?
β Apart from +state
effect there is another way to handle state in a more imperative way as a mutable data class. Just annotate your class with @Model
and all mutable properties referenced in composition context will create a binding which will trigger recomposition of referencing chunk of UI.
π€ During composition, there is static variable currentComposer
maybe better was to make Composables extension functions on top of this Composer context?
βοΈ This is a temporary decision. In the future releases there would be multiple composition roots, each hierarchy would have one. As of extensions, there is only one receiver possible for Kotlin extension functions, so it would be a major drawback to fill that spot by a framework.
π€ I found a bug where to submit it?
βοΈ Post an issue in this tracker
π€ I did not find answer for my question here, what can I do?
βοΈ Join Kotlin Slack community and find a #compose
channel there. Feel free to ask for any help!
If you speak Russian, you also might be interested in joining telegram community dedicated to Declarative UI on Android.