WHY must I PROVIDE a lifecycle owner and registry ...
# compose
v
WHY must I PROVIDE a lifecycle owner and registry owner for a view with composables in it. THIS ONE THING is preventing me from using a service context to launch these views and forcing me to use an activity. BUT I DONT WANT AN ACTIVITY. dealing with the thing is a headache as my thing is an overlay and I want to simply launch the overlay with composables without launching any activities. Even with a lifecycleservice the registry owner is #notThere
a
so what is the question here?
m
c
You CANT show any views from a service Context. YOU need a window do show something on the screen and only an ACTIVTY context has a window.
v
you only need a window manager, that you can get from the system service
Copy code
val wm = getSystemService(WINDOW_SERVICE) as WindowManager
c
Exactly, because only activities are capable of displaying stuff on the screen.
v
you can get a window manager in a service and use that to display information. The problem is that the service by default has no lifecycle owner and no saveStateRegistry owner. This will allow me to display views but they crash because those 2 things are null. This is because compose REFUSES to autohandle that , while base android views basically dont care, at least based on what I got from that previous thread
a
> because only activities are capable of displaying stuff on the screen. this is simply not true. i have built a few apps using overlays with services and Android Views. havent done it with compose though. @Vishnu Shrikar I am sure you have checked already but isn't there a way to make your own lifecycle owner and/or saveStateRegistryowner? this is agenuine question. i dont know if you can
1
v
I managed to get an owner by using a LifecycleService (had to add some package) but the savedStateRegistryowner is where I have no idea. I am getting the owner through a lifecycle observer but saved state registry owner I have no idea
a
have you seen this article? seems like it does exactly what you are after https://www.techyourchance.com/jetpack-compose-inside-android-service/
had a quick look. it does the wiring manually
v
hmm ok ill try this out, maybe the service just does not need to set any of this stuff at all, and since I was setting it to null, it got mad, if this is true, services are heros and activities can suck it
a
it's not easy what you are trying to do because it's uncharted territory. the whole drawing overlays is a hack on android, so my 2c is to keep the scope of what you building to a bare minimum (like display something and hide it and that's it). that's has more to do with Android than Compose though
v
Yeah I dont want to do this either. The complete fuck holes that are tmobile are forcing my hand. Long story short, Visual Voicemail is broken on many phones with tmobile, their support wont help, so now I gotta fix it. sadly this means overlaying the phone app, amongst other annoy8ing things
z
I can confirm that it's possible to do through a service, just some manual work involved!
c
Yeah I am also using Compose outside of an activity by implementing the interfaces myself. It takes a little bit of work but these things can be done
l
This should be asked in #compose-android
👍 1
You're looking for a function with this kind of signature?
Copy code
inline fun @Composable () -> Unit.runInScope(
    context: Context,
    block: (View) -> Unit
)