Check out this great library for event handling in Android apps. It should be what you're looking for.
Android/Java substitute for NSNotificationCenter that does not need a Context reference
-
05-07-2023 - |
質問
So far I have worked on several iOS projects and currently I am porting one of these apps to my first android app.
The data model of iOS Version uses NSNotificationCenter
to broadcast status messages that are used notify the view classes about updates. This works very well and is comfortable since the views do not need to know/see all model classes but only the update messages.
I am trying to do the same on Android. Of course there is no NSNotificationCenter
here but I found the LocalBroadcastManager
to be almost the same. Almost the same, but not exactly the same: The getInstance(...)
methods need a reference to the Context
(e.g. an Activity
). This is a problem because the model classes don't know any Activity
ore Context
.
All information I found say, that if a model class has to know an Activity
it should get a reference to it, e.g. within the constructor. But it would be a realy "ugly" solution to pass down such a reference through the chain of model classes just to be able to post notifications.
Is there any other solution to use NSNotificationCenter
in Android?
解決