Does an equivalent of <include> exist to reuse designs/layouts in Jetpack Compose?

Viewed 591

In normal Xml activity building exists an <include> property that allows reuse designs in different activities like top and bottom bars. I have been searching for something like that in Jetpack Compose but I cannot find anything. I know that @Composable functions work as independent elements that can be used whenever you want, but if I'd used this approach these elements would be reloaded whenever the activity changed.

1 Answers

There's no need: just call the function where you've defined the design/layout you want to use.

In traditional Android layouts, the <include> tag is needed because XML doesn't have any concept of "calling" another XML file. In Compose, every layout and every component is a function, so you can simply call the function wherever you want to reuse that layout.

There's almost no difference between including the contents of a composable function directly inside another composable function and calling that composable function (there are slight differences in the runtime's internal bookkeeping, but generally nothing that users of Compose need to worry about).

In fact, you can mark a composable function inline to achieve this more directly and have the contents inlined into the calling function.

Related