android - Best alternative to Stop an Activity started from a Service -
i started activity following code service:
intent cmactivityintent = new intent(getapplicationcontext(), callmanagementactivity.class); cmactivityintent.setflags(intent.flag_activity_new_task); startactivity(cmactivityintent); in app's manifest launchmode of activity defined android:launchmode="singletask".
i've been looking ways of stopping activity on service's ondestroy() haven't been able find documentation can me. , i've found 1 way of doing it.
i've seen aproach of checking on serviceif activity instanced using activity-class-static-property (public static boolean isinstanced) , sending intent activity finish_activity if is. then, on activity's onnewintent() flag checked , activity finished if flag included.
this approach doesn't feel right me, since intents supposed used start activities , not stopping them.
does know other way of how acomplish this?
many alternatives have been given solve issue:
- use
intent, setextra. - send broadcast intent
service, registerbroadcastreceiveronactivity. - use
localbroadcastmanager(similar previous alternative) - bind
activityservice, passmessengerservicecan use communicateactivity.
none of them have found solution, in fact, since service accesible package, solution i'm sticking observer pattern.
next you'll find code used. activities implement observer interface (my own) , service abstract class observableservice.
observableservice:
public abstract class observableservice extends service { @override public void oncreate() { super.oncreate(); servicestarted = true; observers = new arraylist<observer>(); } @override public void ondestroy() { super.ondestroy(); servicestarted = false; } //--------------------------------------------------------- // // singleton methods // //--------------------------------------------------------- /** * flag set <code>true</code> if service running , * <code>false</code> if not. */ private static boolean servicestarted; /** * used check if service started. * @return <code>true</code> if service running , <code>false</code> * if not. */ public static boolean isservicestarted() { return servicestarted; } //--------------------------------------------------------- // // observable methods constants // //--------------------------------------------------------- /** * used notify observers service has been stopped. */ public static string update_type_service_stopped = "service_stopped"; protected static arraylist<observer> observers; public static void addobserver(observer observer) { observers.add(observer); } public static void removeobserver(observer observer) { observers.remove(observer); } protected static void notifyobservers(string updatetype) { (observer observer : observers) { observer.onobservableupdate(updatetype); } } }
observer:
public interface observer { public void onobservableupdate(string updatetype); } the activities register observers @ onresume() , unregister @ onpause(). isservicestarted() used finish activity if service stopped.
@override protected void onresume() { super.onresume(); if (incallservice.isservicestarted()) { incallservice.addobserver(this); } else { this.finish(); } } @override protected void onpause() { super.onpause(); incallservice.removeobserver(this); } public void onobservableupdate(string updatetype) { if (updatetype.equals(observableservice.update_type_service_stopped)) { this.finish(); } } i chose because found simple , high-performance alternative.
Comments
Post a Comment