Scope: Have to update activity UI in different ways. Update depends on broadcasts received from service. Problem: There are two common ways to find out which way UI should be updated:
The 2nd ways seems to be more elegant and more understandable. But I wonder if it will consume more memory. What would you recommend? Thanks!
Android BroadcastReceiver is a dormant component of android that listens to system-wide broadcast events or intents. When any of these events occur it brings the application into action by either creating a status bar notification or performing a task.
Broadcast in android is the system-wide events that can occur when the device starts, when a message is received on the device or when incoming calls are received, or when a device goes to airplane mode, etc. Broadcast Receivers are used to respond to these system-wide events.
There is also a 5-10 second limit, after which Android will basically crash your app. However, you cannot reliably fork a background thread from onReceive() , as once onReceive() returns, your process might be terminated, if you are not in the foreground.
inject(this, context); in onReceive method. Here is the receiver so you don't have to do that, the trick is extending from DaggerBroadcastReceiver : class MyReceiver : DaggerBroadcastReceiver() { @Inject lateinit var repository: MyRepository override fun onReceive(context: Context?, intent: Intent?) { super.
It should not make a major difference either way.
I agree with CommonsWare. From a perfomance standpoing this isn't really something you need to worry about. That said, I'd probably go with 2nd way for the sole reason that it will make your code more modular thus improving maintainability.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With