https://kotlinlang.org logo
#android
Title
# android
d

David Rincon-Cruz

01/12/2021, 12:15 AM
I have an app that needs to print using specific data formats (not just sending pdfs through google print service). In my
MainActivity
, I create a class called `PrintManager`where I set up most of the logic to run on a bunch of never-ending coroutines using channels to act as a producer-consumer queues. So, whenever you need to print something,
PrintManager
will enqueue it in the PrintChannel, and some coroutine is actively trying to receive from it in order to form a TCP connection and send the encoded bitstream over. My issue is that this seems rather complicated and kind of wraps the lifecycle of the
PrintManager
to a single activity since many of my printing sdks require a context be passed in order to create the TCP connections. Is there a way to offload this work to a background service that runs in parallel to the main app? I’d like to be able to dependency-inject
PrintManager
into all the Fragments/ViewModels that need to call it, rather than passing them in as a field of MainActivity.