Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetpack Compose internal design: Why draw on a Canvas, instead of re-using native Views?

Tags:

This question builds on top of : How does jetpack compose work under the hood

Jetpack Compose is based on AndroidComposeView which inherits from ViewGroup and serves as the root of Compose's own widget tree. As such, the tree as a whole can be integrated into existing views, but individual nodes are not Android Views. The transition between Android views and Compose UI widgets is sharp: some key classes are re-implemented for Compose (like Canvas) and the UI widgets of Compose are (currently) not visible in the Layout inspector.

Jetpack Compose renders its widgets onto a canvas and uses some neat tricks to apply state changes. As far as I understand, this is entirely orthogonal to the existing layout engine and you are not supposed to switch back and forth between native views and compose widgets. This would be a stark contrast to SwiftUI:

SwiftUI works seamlessly with the existing UI frameworks on all Apple platforms. For example, you can place UIKit views and view controllers inside SwiftUI views, and vice versa.

I have little to no knowledge on the iOS side of things. Maybe UIkit was simply better suited to interface with SwiftUI. But if that is not the case, and the separation of Jetpack Compose from native widgets is not forced by internal constraints, it seems like a strange design decision.

Now, my question is: Why is Jetpack Compose based on its own rendering path (based on a custom Canvas implementation), instead of somehow re-using native views? Does this have hidden advantages or was it simply not possible to bend the existing views to a reactive programming pattern? It seems to me as if this will facilitate potential multi-platform efforts in the long run, but lead to more fragmentation of App development in the short term.

like image 297
lhk Avatar asked Nov 25 '19 16:11

lhk


People also ask

Does jetpack compose use views?

Jetpack Compose is designed to work with the established View-based UI approach. If you're building a new app, the best option might be to implement your entire UI with Compose.

What is recomposition in jetpack compose?

Recomposition is when Jetpack Compose re-executes the composables that may have changed in response to state changes, and then updates the Composition to reflect any changes. A Composition can only be produced by an initial composition and updated by recomposition.

Why is jetpack compose better?

Jetpack Compose is a modern toolkit that allows us to build our screens in a declarative approach writing less code. Android UI Development is now more powerful and more decoupled. Before Jetpack Compose, we were using XML layouts to build the native UI.

Is jetpack compose stable now?

New stable features and APIsSeveral features and APIs were added as stable. Highlights include: The APIs LazyHorizontalGrid and LazyVerticalGrid let you place lists of items in a grid. These APIs already existed in Compose 1.1 but were marked as @Experimental .


1 Answers

I believe one of the reasons why rendering own UI was picked is because Compose is aiming to be multi-platform and similar to Flutter, we might be able to write iOS supported UIs with Compose

like image 145
Martin Nowosad Avatar answered Sep 29 '22 11:09

Martin Nowosad