4

I can think of 2 options but neither of them sounds best-practice :

  1. Extending LiveData. But then it would hold a reference to Context, and as Google states this is a very bad design to hold any reference to Views or Context inside ViewModel.

  2. Extending LifecycleObserver, like this : https://stackoverflow.com/a/44449738

What is the recommended and right way of using broadcast in MVVM ?

BVtp
  • 2,308
  • 2
  • 29
  • 68
  • Please provide the use-case of the broadcast receiver.. why do you need the receiver in MVVM architecture ? – Santanu Sur Mar 18 '19 at 12:58
  • Not a specific use-case I've encountered. But I just wanted to know what happens if I do decide I need a broadcast receiver while using MVVM ? I mean, MVVM architecture doesn't replace BroadcastReceivers in any way, right ? – BVtp Mar 18 '19 at 13:01
  • BroadcastReceivers are just an event source that can be registered into the system. You can create a class that receives the notification, emits it onto an EventBus or PublishRelay (or just regular Observer pattern i guess), and other classes get an instance of this class in order to listen to events coming from the broadcast receiver. – EpicPandaForce Mar 19 '19 at 08:35
  • I saw [this](https://stackoverflow.com/a/54264538/6541168) where broadcast managers are being deprecated? what other techniques do we have to communicate between activity and service? – ateebahmed Mar 28 '19 at 15:59

0 Answers0