Jetpack Compose Crash when adding view to window manager

Original link: http://i.lckiss.com/?p=8056

The error is something like this:

 java.lang.IllegalStateException: ViewTreeLifecycleOwner is not present in this window. Use ComponentActivity, FragmentActivity or AppCompatActivity to configure ViewTreeLifecycleOwner automatically, or call ViewTreeLifecycleOwner.set() for this View or an ancestor in the same window.

In fact, I discovered this problem when I first came into contact with it a few months ago, but the purpose at that time was to pop up the window, but there was not much content retrieved at that time. The official also gave the compose version of the popup. After trying it, I gave up this problem. . Until recently, I wanted to make a floating window. Because of the convenience of Compose’s rounded corners, backgrounds, gradient colors, etc. in the UI, I wanted to try it again, so I encountered this error again. The keyword of this search is directly WindowManager add ComposeView, so it was solved quickly.

Solution :

 class ComposeViewLifecycleOwner : SavedStateRegistryOwner {
private var mLifecycleRegistry: LifecycleRegistry = LifecycleRegistry(this)
private var mSavedStateRegistryController: SavedStateRegistryController = SavedStateRegistryController.create(this)

/**
* @return True if the Lifecycle has been initialized.
*/
val isInitialized: Boolean
get() = true

override fun getLifecycle(): Lifecycle {
return mLifecycleRegistry
}

fun setCurrentState(state: Lifecycle.State) {
mLifecycleRegistry.currentState = state
}

fun handleLifecycleEvent(event: Lifecycle.Event) {
mLifecycleRegistry.handleLifecycleEvent(event)
}

override fun getSavedStateRegistry(): SavedStateRegistry {
return mSavedStateRegistryController.savedStateRegistry
}

fun performRestore(savedState: Bundle?) {
mSavedStateRegistryController.performRestore(savedState)
}

fun performSave(outBundle: Bundle) {
mSavedStateRegistryController.performSave(outBundle)
}
}

extension method :

 fun AbstractComposeView.addToLifecycle() {
val viewModelStore = ViewModelStore()
val lifecycleOwner = ComposeViewLifecycleOwner()
lifecycleOwner.performRestore(null)
lifecycleOwner.handleLifecycleEvent( Lifecycle.Event.ON_CREATE )
ViewTreeLifecycleOwner.set(this, lifecycleOwner)
ViewTreeViewModelStoreOwner.set(this) { viewModelStore }
ViewTreeSavedStateRegistryOwner.set(this, lifecycleOwner)
val coroutineContext = AndroidUiDispatcher.CurrentThread
val runRecomposeScope = CoroutineScope (coroutineContext)
val reComposer = Recomposer(coroutineContext)
this. compositionContext = reComposer
runRecomposeScope.launch {
reComposer.runRecomposeAndApplyChanges()
}
}

Usage :

 val composeView = SomeComposeView(context) composeView.addToLifecycle() windowManager.addView(composeView, layoutParam)

Pit encountered :

At the beginning, I simply added Compose, but I couldn’t reorganize it. After reading the comments, I realized that a part of the code was missing (the code above is complete), so I will post it here:

 val coroutineContext = AndroidUiDispatcher.CurrentThread val runRecomposeScope = CoroutineScope(coroutineContext) val recomposer = Recomposer(coroutineContext) composeView.compositionContext = recomposer runRecomposeScope.launch {     recomposer.runRecomposeAndApplyChanges() }

Reference :

Jetpack Compose OverlayService.

This article is reproduced from: http://i.lckiss.com/?p=8056
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment