I have class that extends ‘IntentService’ – this class occasionally receives data from a server, and when it does I need to pass this data to the current running Activity.
To achieve this, I’ve added a static instance of my Activity to the IntentService, along with a registerRecipient method that the Activity calls, passing in self. When the IntentService gets a message from the server, it just calls the appropriate notification method on its static instance of the Activity (in which I set an EditText view’s text property via runOnUiThread).
Aside from the general problem of using static variables instead of instances, is this a sound way to do this? One of my colleagues suggested that this static instance is still subject to being garbage-collected, which would hose the whole scenario.
For this scenario you do not need a static member variable of your
Activity. What you need is aBroadcastReceiver, which corresponds with this question.