Closed luiisca closed 1 week ago
so apparentely widgets do mount without issue, but composables just crash the app
read a bit about what windows are and how the windowManager
works:
https://stackoverflow.com/a/22442702/16441651
They show an example using old xml layouts, I guess it should work, but I'm trying to use composables.
went in a bit of a rabbit hole reading android's documentation and source code. And I think the reason behind the crashing is because I'm passing the wrong context to ComposeView
, I'm passing a Service
class but I think it's expecting a View
class. This could be confirmed by the [kinda broken] link this SO discussion points to: https://stackoverflow.com/questions/65755763/inputmethodservice-with-jetpack-compose-composeview-causes-composed-into-the. It basically says that ComposeView
needs to be attached to a ViewTreeLifecycleOwner
which is only implemented by ComponentActivity
and others by default, none being Service
. So the solution seems to be to implement those methods ComposeView
is expecting.
ok, I feel like I now understand better this "attached to ViewTreeLifecycleOwner
statement". So apparentely Compose will look at the view is about to "render" for a LifecycleOwner
(a class that manages the View), this LifecycleOwner
is already "provided" by classes like ComponentActivity
automatically but this is not the case for a Service, that's why we need to create our own custom class that extends LifecycleOwner
and then bind or attach the View
to it, so Compose could find it when looking on View
. Not completely sure how this lookup process work, but I think this is enough.
ok, i've implemented the fix. Now I bind a LifecycleOwner
to the FloatView
composable before passing it to the window manager
private fun composeFloatView(): View {
val floatView = ComposeView(this).apply {
this.setContent {
FloatView()
}
}
floatViewLifecycleOwner.attachToDecorView(floatView)
return floatView
}
LUI-382 fix: windowManager.addView crashes every time