26
Introduction to Jetpack Compose

Throughout this book, you have built UIs using View classes and XML layout files. These APIs are provided by the Android OS and are part of the Android framework UI toolkit. Colloquially, we refer to these APIs as “framework views.”

Building UIs with framework views has been the standard for making an Android app since the first release of the OS. But in recent times, the framework view system has left much to be desired. For starters, it is built into the OS itself. This means that getting the latest features requires users to update their entire OS, which is not always an option. It also requires developers to bump their apps’ minimum SDK level, leaving behind users who are not able to upgrade.

Also, Android’s framework UI toolkit is based around ideas like the view hierarchy, view classes that extend from one another, and updating the state of your view manually, line by line. Meanwhile, many front-end UI frameworks have moved on to more modern approaches that make building UIs easier and more streamlined.

To address both of these issues, Google has created a new UI toolkit called Jetpack Compose. Jetpack Compose replaces the built-in framework UI toolkit. It is part of the Jetpack suite of libraries, so a UI built with Compose is entirely separate from the Android OS. And because it is separate, you can get updates to Compose just as you would any external library.

Compose is designed in Kotlin (and, in fact, is exclusively available in Kotlin) and is a declarative UI framework. The benefits of a declarative UI toolkit will become apparent in the next chapter, when you learn about UI state in Compose. To give you a teaser, though: Compose automatically updates your UI when your application state changes. You declare your UI how you want it to appear at all times, and Compose will make it so.

This marks a radical departure from what you are used to. Jetpack Compose does not let you store a reference to any of your UI elements, which means no View Binding, no imperative UI updates – even the time-honored findViewById() function is not available in Compose. Initially, you might find Compose a bit tricky to reason about, since it requires you to think about your UI differently. However, as you will see in the next chapter, Compose plays well with the modern Android programming paradigms you have used throughout this book like making your UI state observable and updating UI elements reactively.

In our opinion, Jetpack Compose offers a more elegant and concise set of tools to build UIs than what is available in the framework UI toolkit. Google is also emphasizing Jetpack Compose, and we expect that many apps in the future will exclusively use Compose and leave framework views behind.

You may be wondering, If Compose is the latest and greatest, why bother learning about the framework UI toolkit as well? We are glad you asked.

Jetpack Compose hit version 1.0 and went stable in the summer of 2021. Since then, the Android development community has begun to transition to Compose – but a transition of this size takes time. If you are just starting your journey as an Android developer, you likely need to be familiar with both UI frameworks, as many existing apps, libraries, code snippets, and examples still rely on framework views and will for some time.

Over the next four chapters, we will walk you through the basics of building UIs in Compose. We will not be able to cover every feature Compose offers, but you will end with a solid foundation to build UIs in Compose. You can find more information about Jetpack Compose on its documentation page, developer.android.com/​jetpack/​compose/​documentation.

Although the UI code in this project will be different than what you have been seeing, fear not. You are still working on Android – everything you have learned so far will be helpful as you venture into this new territory. Let’s get started.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.197.213