one question how can i bind a service before setco...
# compose
i
one question how can i bind a service before setcontent in MainActvity? i have a exoplayer instance i create in service. and want to give ist to my mainscreen in the set content in oncreate? i implement the way google recommend but in oncreate my player instance is every time null
a
Got a reference link/gist or similar? We could probably give some suggestions
1
this is my MainActvity
https://gist.github.com/izyaboi/5d53cd3dada60fb2d024255576e5af97#file-playernotificationservice and this the service what i want is a valid instance for my composable main screen
a
taking a look (if you add the .kt extension to the filenames in the gist it syntax highlights 🙂 )
👍 1
i
oh sorry , updated!
👍 1
a
sorry, got distracted
a few options here depending on how you want to scope this
if you want to keep this structure and have the service own the player, do the binding and keep a
mutableStateOf<MyService?>(null)
in the activity and assign it to something non-null once the binding is complete
instead of using the
lateinit var
for it
then use the value in your composable functions in the
setContent
and pass it down
to hide the latency of the service binding you can do a few things; you can have a shim interface that just reports "loading" or similar for anything you ask it and maybe queue commands to it, and have it keep that
mutableStateOf
as an internal implementation detail that you delegate to when it's present
you can just keep it nullable and have your composables know to treat that as an unbound state
or you could scope the player outside of the service entirely; scoping it to either your Application object or some other singleton that you lazily initialize, which will probably be faster than the service binding (i.e. it can be synchronous)
the service is already a singleton as far as the system is concerned; android won't create more than one instance of it when it's bound
i
the second option to scope out the player from the PlayerNotificationService is the one u prefered? good tip with mutableStateOf i give it a try!
a
yeah I'd probably scope the player outside of the service personally. You still want to have the service exist because it gives you the foreground notification surface for an audio app
but it doesn't have to be the thing that owns the player
the reason to scope the player to a service like that is if you're running the service in its own process independent of the main ui process
i
thx for the explaination . i do some work and test it again. if there a some problems i can ask u ?
a
feel free to ask again in this thread or in a new thread in this channel, yes. DMs don't work as well because then others who might have similar questions can't benefit from being able to read it 🙂