This is pretty good using an abstract class where you handle all the actions and just send a callback to your activity. Using this example in your previous question seems to me like an EventBus.
And it's even better to use a special class and interfaces instead of an abstract class, because you can use FragmentActivity, AppCombatActivity, etc.
For example, you have your own class, which receives the result from your service and sends all the actions registered to it. Calling the result from network requests with interfaces:
public class NetRequestReceiver extends BroadcastReceiver { private List<Activities> registeredActivities; public static void getInstance () { //Continue singleton initialing! //.... } @Override public void onReceive(Context context, Intent intent) { for (Activity act : registeredActivities) { if (act instanceOf ReceivingCallback) { act.onReceiveMessage(intent); } else throw Exception("Activity missing ReceivingCallback"); } } public void registerActivity (Activity, activity) { if (!registeredActivities.contains(activity)) { registeredActivities.add(activity); } } public void unRegisterActivity (Activity, activity) { if (registeredActivities.contains(activity)) { registeredActivities.remove(activity); } } public interface ReceivingCallback { onReceiveMessage (Intent intent); } }
Then in all your actions add the next listener. But (!) Do not forget the registrar above in your service to get the result!
public class MainActivity extends Activity implements NetRequestReceiver.ReceivingCallback { public void onStop () { super.onStop() NetRequestReceiver.getInstance().unRegisterActivity(this); } public void onResume () { super.onResume() NetRequestReceiver.getInstance().registerActivity(this); } @Override public onReceiveMessage (Intent intent) {
What do you think using the design above? Now we have one receiver and callback as an interface. Thus, you can use Fragment, Activity, FragmentActivity and another class to get the result from the service via Broadcast and (!) Without copying, pasting the same behavior!
It also looks good, because we share different layers - presentation, view and receiver. You are invoking a network request in the Service. This service sends the result to Broadcast, and then sends the data to all registered activities.
Yes, this is similar to EventBus, but based on your question, this is just what you need to listen for the connection from the service in different actions and with a better structure.