Skip to content

Instantly share code, notes, and snippets.

@andreymusth
Last active March 10, 2023 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreymusth/802358e203d742702eb53da14a10fde9 to your computer and use it in GitHub Desktop.
Save andreymusth/802358e203d742702eb53da14a10fde9 to your computer and use it in GitHub Desktop.
set content 1
public fun ComponentActivity.setContent(
parent: CompositionContext? = null,
content: @Composable () -> Unit
) {
val existingComposeView = window.decorView
.findViewById<ViewGroup>(android.R.id.content)
.getChildAt(0) as? ComposeView
if (existingComposeView != null) with(existingComposeView) {
setParentCompositionContext(parent)
setContent(content)
} else ComposeView(this).apply {
// Set content and parent **before** setContentView
// to have ComposeView create the composition on attach
setParentCompositionContext(parent)
setContent(content)
// Set the view tree owners before setting the content view so that the inflation process
// and attach listeners will see them already present
setOwners()
setContentView(this, DefaultActivityContentLayoutParams)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment