```fun ComponentActivity.mySetContent( parent:...
# compose
m
Copy code
fun ComponentActivity.mySetContent(
    parent: CompositionContext? = null,
    content: @Composable () -> Unit,
) {
    val existingView = this.window.decorView
        .findViewById<ViewGroup>(android.R.id.content)
        .getChildAt(0) as? ComposeView
    val compositionDest = (existingView ?: ComposeView(this)).also { view ->
        view.setParentCompositionContext(parent)
        view.setContent(content)
    }
    if (existingView != compositionDest) {
        this.setContentView(compositionDest, ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        ))
        this.window.decorView.also { decorView ->
            if (decorView.findViewTreeLifecycleOwner() == null) {
                decorView.setViewTreeLifecycleOwner(this)
            }
            if (decorView.findViewTreeViewModelStoreOwner() == null) {
                decorView.setViewTreeViewModelStoreOwner(this)
            }
            if (decorView.findViewTreeSavedStateRegistryOwner() == null) {
                decorView.setViewTreeSavedStateRegistryOwner(this)
            }
        }
    }
}
I’m reimplementing parts of Jetpack Compose to get a better idea of how it works. My understanding is that
existingView
is only not
null
if
mySetContent
has already been called on the current
ComponentActivity
object; that the
ComposeView
created in the assignment to
compositionDest
gets registered in the
if
body via the call to
this.setContentView
the first time the method is called; and that it’s this
ComposeView
object that every subsequent call to the method on the same instance gets access to via
existingView
. Am I correct, and if so, there should be nothing wrong with swapping out
ComposeView
for
MyComposeView
(my implementation) here, right?
m
I’m reimplementing parts of Jetpack Compose to get a better idea of how it works. Wow, that’s ambitious 🧐.
☝️ 1
z
In the future, please keep code snippets more than a few lines to the thread. This helps keep the main channel more readable. Thanks!
m
What do you mean?
Sorry, I’m not familiar with how Slack works
Did I do something wrong? I apologise. I will delete and repost the question if necessary
Figured out how to create a thread, will start threads for long questions from now on – thanks for the heads-up
z
No you’re fine, just a heads up for future posts 🙂