Jetpack Compose will be the new declarative UI to build Android apps. Based on this new paradigm, let's see how we can use it with some simple samples!
data class Activity( @DrawableRes val picture: Int, val displayName: String, val type: String, val price: Int ) class ActivityListAdapter(val items: MutableList) : RecyclerView.Adapter() { val onClick: LiveData get() = this._onClick private val _onClick: MutableLiveData by lazy { return@lazy MutableLiveData() }
@Composable fun MyExample() { // Returns the same instance as long as the activity is alive, // just as if you grabbed the instance from an Activity or // Fragment val viewModel: MovieViewModel = viewModel() } @Composable fun MyExample2() { // Same instance as in MyExample val viewModel: MovieViewModel = viewModel() }
COMPILER RUNTIME UI FOUNDATION-LAYOUT ANIMATION FOUNDATION MATERIAL 3 Transf m @C posable functi s Pro amming model, state management and runtime f the c pose c pil plugin Fundamental c p ents Basics lay ts like B , R and C umn C p ents animati s Basics c p ents and int acti s Implementati of Mat ial Design specificati s Implementati of Mat ial Y
COMPILER RUNTIME UI FOUNDATION-LAYOUT ANIMATION FOUNDATION MATERIAL 3 Transf m @C posable functi s Pro amming model, state management and runtime f the c pose c pil plugin
COMPILER RUNTIME UI FOUNDATION-LAYOUT ANIMATION FOUNDATION MATERIAL 3 Transf m @C posable functi s Pro amming model, state management and runtime f the c pose c pil plugin WEB-CORE-RUNTIME WEB-CORE WIDGETS Runtime dedicated to web apps to gen ate DOM tags Basics native web c p ents Modifi s and lay ts
WHY COMPOSE WEB? ▸ Compose Canvas rendering not compatible (yet) with web technologies ▸ Compose Web emit DOM UI for NPM librairies compatibility and SEO
@Composable actual fun Font( fontName: String, weight: FontWeight, style: FontStyle ): Font { val context = LocalContext.current val id = context.resources .getIdentifier(fontName, "font", context.packageName) return Font(id, weight, style) } SOURCE SET androidMain
WHY SHOULD CARE ABOUT COMPOSE? ▸ Android UI framework never changed since 2008 ▸ Need to wait very long time before bug fixes deployed in production ▸ Need to worry about keeping the View hierarchy as flat as possible ▸ Unbundled from OS ▸ Exit View and Fragment ▸ Clarify state ownership and event handling ▸ Writing less code