-
-
Save andreymusth/802358e203d742702eb53da14a10fde9 to your computer and use it in GitHub Desktop.
set content 1
This file contains 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
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