Socheat KHAUV
04/28/2021, 8:57 PMclass FloatingService : Service() {
private var windowManager: WindowManager? = null
override fun onCreate() {
super.onCreate()
windowManager = ContextCompat.getSystemService(this, WindowManager::class.java)
var view = LayoutInflater.from(this).inflate(R.layout.player_popup_close_overlay, null, false)
val closeOverlayLayoutParams = buildCloseOverlayLayoutParams()
var composeView = view.findViewById<ComposeView>(R.id.compose_view)
composeView.setContent {
Button(onClick = { }) {
Text(text = "Click Me")
}
}
Objects.requireNonNull(windowManager)?.addView(view, closeOverlayLayoutParams)
}
}
Socheat KHAUV
04/28/2021, 9:00 PMAdam Powell
04/28/2021, 9:08 PMViewTreeLifecycleOwner
set on the root view of the new window you're creatingAdam Powell
04/28/2021, 9:08 PMSocheat KHAUV
04/28/2021, 9:39 PMViewTreeLifecycleOwner.set()
and ViewTreeSavedStateRegistryOwner.set()
.
at androidx.compose.ui.platform.AbstractComposeView.checkViewTreeOwners(ComposeView.android.kt:179)
at androidx.compose.ui.platform.AbstractComposeView.ensureCompositionCreated(ComposeView.android.kt:195)
at androidx.compose.ui.platform.AbstractComposeView.onAttachedToWindow(ComposeView.android.kt:235)
at android.view.View.dispatchAttachedToWindow(View.java:20479)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3489)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2417)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1952)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8171)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:972)
at android.view.Choreographer.doCallbacks(Choreographer.java:796)
at android.view.Choreographer.doFrame(Choreographer.java:731)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)Adam Powell
04/28/2021, 9:53 PMViewTreeLifecycleOwner.set
on the root view of that window (the one you add to the window manager) to set a LifecycleOwner for the ComposeView to locate and useAdam Powell
04/28/2021, 9:54 PMLifecycleRegistry
instance as your implementation of the Lifecycle
object returned by it, and you should drive its lifecycle events as appropriate for your service to emulate an activity lifecycleSocheat KHAUV
04/28/2021, 11:53 PMcomposeView.setContent {
Button(onClick = { Log.d("test","from overlay window")}) {
Text(text = "Click Me 1")
}
}
Socheat KHAUV
04/29/2021, 12:56 AMViewTreeLifecycleOwner.set(view, owner)
ViewTreeSavedStateRegistryOwner.set(view, owner)
I also need to give a service binder and service connection, so that I could add an activity reference (life cycle owner) back to my service class.
Thanks you for helping.Socheat KHAUV
05/04/2021, 11:19 AMAdam Powell
05/04/2021, 3:47 PMSocheat KHAUV
05/04/2021, 5:18 PMvar composeView = overlayView!!.findViewById<ComposeView>(R.id.compose_view)
composeView.setContent {
SmallWindowScreen()
}
SmallWindowScreen it is a composeable function which use for display overlay window
there is a button âOpenâ, click on that button will open MainActivity
var intent = Intent(context, MainActivity::class.java)
context.startActivity(intent)
and the problem is that, if we click that button to many time, that button would freeze,
and we click only one and wait, it will open activity some later (mabye 10seconds)Adam Powell
05/04/2021, 5:42 PM