Overlay Compose View is not visible after removed and added again (Service, WindowManager)

115 Views Asked by At

Behavior:

when I remove the compose view and add again, it's not show anything. Like in the video below, when I click popBack button from the expanded view, the small bubble is expected to show again.

Steps to Reproduce:

  • Upgrade compose version from 1.3.1 to 1.5.0, in my case androidx.compose.foundation:foundation
  • The code below is a custom lifecycle owner class which helps me manage ComposeView in previous versions. You can see more information here Floating-Bubble-View
internal class ComposeLifecycleOwner : SavedStateRegistryOwner, ViewModelStoreOwner {

    private var _view: View? = null
    private var recomposer: Recomposer? = null
    private var runRecomposeScope: CoroutineScope? = null
    private var coroutineContext: CoroutineContext? = null

    init {
        coroutineContext = AndroidUiDispatcher.CurrentThread
    }

    fun onCreate() {

        savedStateRegistryController.performRestore(null)
        lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE)

        runRecomposeScope?.cancel()

        runRecomposeScope = CoroutineScope(coroutineContext!!)
        recomposer = Recomposer(coroutineContext!!)
        _view?.compositionContext = recomposer

        runRecomposeScope!!.launch {
            recomposer!!.runRecomposeAndApplyChanges()
        }
    }

    fun onStart() {
        try {
            lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START)
        } catch (e: Exception) {
            /**
             * java_vm_ext.cc:594] JNI DETECTED ERROR IN APPLICATION: JNI CallVoidMethodV called with pending exception java.lang.IllegalStateException: no event up from DESTROYED
             * java_vm_ext.cc:594] at void androidx.lifecycle.LifecycleRegistry.forwardPass(androidx.lifecycle.LifecycleOwner) (LifecycleRegistry.java:269)
             *
             * */
//            e.printStackTrace()
        }
    }

    fun onResume() {
      lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_RESUME)
    }

    fun onPause() {
       lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    }

    fun onStop() {
        lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP)
    }

    fun onDestroy() {
     lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
        savedStateRegistryController.performSave(Bundle())
//        runRecomposeScope?.cancel()
    }

    fun attachToDecorView(view: View?) {
        if (view == null) return

        _view = view

//        ViewTreeLifecycleOwner.set(view, this)
        view.setViewTreeLifecycleOwner(this)
        view.setViewTreeViewModelStoreOwner(this)
        view.setViewTreeSavedStateRegistryOwner(this)
    }

    private val lifecycleRegistry: LifecycleRegistry = LifecycleRegistry(this)
    override val lifecycle: Lifecycle get() = lifecycleRegistry
//    override fun getLifecycle(): Lifecycle = lifecycleRegistry

    private val savedStateRegistryController = SavedStateRegistryController.create(this)
    override val savedStateRegistry: SavedStateRegistry get() = savedStateRegistryController.savedStateRegistry

    private val store = ViewModelStore()
    override val viewModelStore: ViewModelStore get() = store
}

ComposeView issue in 1.5.0

Additional Information:

Jetpack Compose version: 1.5.0

Jetpack Compose component(s) used: "androidx.compose.foundation:foundation:1.5.0"

Kotlin version: 1.7.20

0

There are 0 best solutions below