Android service crashes after app is swiped out of the recent apps list -


i have service gets started (not bound) activity. if activity gets destroyed (e.g. pressing button), service continues run, of course intended. however, if swipe activity out of 'recent apps' list, service gets restarted immediately. reproducible, every time activity/app swiped out of list, there new call service's oncreate-method. no call ondestroy in between!

first thought service gets killed android, though saw no reason kill (neither activity nor service resource consuming things, in fact minimalistic , nothing). noticed service crashes.

v/mainactivity(856): ondestroy // swipe out of list i/activitymanager(287): killing 856:com.example.myapp/u0a10050: remove task w/activitymanager(287): scheduling restart of crashed service com.example.myapp/.testservice in 5000ms 

the code not noteworthy, here is

activity:

public class mainactivity extends activity {      private static final string tag = "mainactivity";      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         log.v(tag, "oncreate, starting service...");         startservice(new intent(this, testservice.class));     }      @override     protected void onstart() {         super.onstart();         log.v(tag, "onstart");     }      @override     protected void ondestroy() {         super.ondestroy();         log.v(tag, "ondestroy");     }      //[...] } 

service:

public class testservice extends service {      private static final string tag = "service";      // onbind omitted      @override     public int onstartcommand(intent intent, int flags, int startid) {         log.v(tag, "onstartcommand");         return super.onstartcommand(intent, flags, startid);     }      @override     public void ondestroy() {         super.ondestroy();         log.v(tag, "ondestroy");     }  } 

in short: service independent of activity's lifecycle, long don't swipe out app of recent apps list. in case, service gets restarted without call ondestroy.

every time happens, not state of service, work service doing lost. want know why swipe reason this.

swiping app recent tasks list kills operating system process hosts app. since service running in same process activities, kills service. not call ondestroy() on service. kills process. boom. dead. gone. service does not crash.

since service returned start_sticky call onstartcommand(), android recognizes service should restarted , schedules restart of killed service. however, when service restarted in newly created process (you can see oncreate() called in service), have start work on again.

rule #1: don't ever swipe apps recent tasks list ;-)


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -