ViewCompositionStrategy |
Description |
---|---|
DisposeOnDetachedFromWindow |
The Composition will be disposed when the underlying ComposeView is detached from the window. Has since been superseded by DisposeOnDetachedFromWindowOrReleasedFromPool .Interop scenario: * ComposeView whether it’s the sole element in the View hierarchy, or in the context of a mixed View/Compose screen (not in Fragment). |
DisposeOnDetachedFromWindowOrReleasedFromPool (Default) |
Similar to DisposeOnDetachedFromWindow , when the Composition is not in a pooling container, such as a RecyclerView . If it is in a pooling container, it will dispose when either the pooling container itself detaches from the window |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2022 Google LLC. | |
// SPDX-License-Identifier: Apache-2.0 | |
@Preview( | |
name = "Small font", | |
group = "Font scales", | |
fontScale = 0.5f | |
) | |
@Preview( | |
name = "Large font", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2023 Google LLC. | |
// SPDX-License-Identifier: Apache-2.0 | |
// Change ViewCompositionStrategy to DisposeOnViewTreeLifecycleDestroyed | |
val composeView = ComposeView(context = context) | |
composeView.setViewCompositionStrategy( | |
ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2023 Google LLC. | |
// SPDX-License-Identifier: Apache-2.0 | |
class MyFragment : Fragment() { | |
// … | |
override fun onCreateView( | |
inflater: LayoutInflater, | |
container: ViewGroup?, | |
savedInstanceState: Bundle? | |
) = ComposeView(requireContext()).apply { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2023 Google LLC. | |
// SPDX-License-Identifier: Apache-2.0 | |
class MyCustomView @JvmOverloads constructor( | |
context: Context, | |
attrs: AttributeSet? = null, | |
defStyle: Int = 0 | |
) : AbstractComposeView(context, attrs, defStyle) { | |
init { |
Benefit | Summary |
---|---|
Improve the developer experience | Allow your consumers to take advantage of the benefits of Compose by providing an intuitive and declarative API for integrating your library. |
Reduce boilerplate code | Reduce the code your consumers have to write when using your library. Instead of having to use interoperability APIs, your consumers can directly use a declarative API through composable functions you provide. |
Fewer bugs | It can be challenging to switch between Compose's declarative style of thinking and View's imperative style of thinking. As a result, when consumers write interoperability code to interact with your View-based library, they can easily introduce bugs. Adding support for Compose guarantees correct interoperability, as this is not the responsibility of the consumer anymore. |
Future-proof | Compose is the modern recommended UI framework for building Android apps. By supporting Compose, you are preparing your library for a Compose-first fut |