java - Adding Foreground Service to Activity causes exception when calling onDestroy -


i have simple foreground service follows, shows foreground notification:

public class foregroundlocationservice extends service {      private final string tag = this.getclass().getsimplename();      @override     public void oncreate() {         super.oncreate();          bitmap icon = bitmapfactory.decoderesource(getresources(),                 r.drawable.bullhorn_24dp);          notification notification = new notificationcompat.builder(this)                 .setcontenttitle("signed in")                 .setticker("signed name123")                 .setcontenttext("you broadcasting location")                 .setsmallicon(r.drawable.broadcast_notifcation)                 .setlargeicon(bitmap.createscaledbitmap(icon, 128, 128, false))                 .setongoing(true).build();          startforeground(serviceconstants.notification_id.foreground_service, notification);      }      @override     public void ondestroy() {         super.ondestroy();     }      @nullable     @override     public ibinder onbind(intent intent) {         return null;     } } 

i can start in app calling in activity:

intent service = new intent(this, foregroundlocationservice.class);  startservice(service); 

this displays following image, expected: example of emulator

the issue when go close app via holding phones home button , swiping app closed. activity’s ondestroy() method called, instead of leaving foreground service running , closing app, following error (and foreground service stops):

fatal exception: main   process: uk.cal.codename.projectnedry, pid: 16495   java.lang.runtimeexception: unable destroy activity {uk/uk.companyscope.companynavigationactivity.companynavigationactivity}: java.lang.indexoutofboundsexception: index: 0, size: 0       @ android.app.activitythread.performdestroyactivity(activitythread.java:4203)       @ android.app.activitythread.handledestroyactivity(activitythread.java:4221)       @ android.app.activitythread.-wrap6(activitythread.java)       @ android.app.activitythread$h.handlemessage(activitythread.java:1538)       @ android.os.handler.dispatchmessage(handler.java:102)       @ android.os.looper.loop(looper.java:154)       @ android.app.activitythread.main(activitythread.java:6119)       @ java.lang.reflect.method.invoke(native method)       @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:886)       @ com.android.internal.os.zygoteinit.main(zygoteinit.java:776)    caused by: java.lang.indexoutofboundsexception: index: 0, size: 0       @ java.util.arraylist.set(arraylist.java:427)       @ android.support.v4.app.fragmentmanagerimpl.makeinactive(fragmentmanager.java:1662)       @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1452)       @ android.support.v4.app.fragmentmanagerimpl.movefragmenttoexpectedstate(fragmentmanager.java:1528)       @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1595)       @ android.support.v4.app.fragmentmanagerimpl.dispatchdestroy(fragmentmanager.java:2951)       @ android.support.v4.app.fragmentcontroller.dispatchdestroy(fragmentcontroller.java:271)       @ android.support.v4.app.fragmentactivity.ondestroy(fragmentactivity.java:390)       @ android.support.v7.app.appcompatactivity.ondestroy(appcompatactivity.java:209)       @ uk.demo.genericmodelviewpresenter.genericmvp.genericmvpactivity.ondestroy(genericmvpactivity.java:100)       @ uk.companyscope.companynavigationactivity.companynavigationactivity.ondestroy(companynavigationactivity.java:533)       @ android.app.activity.performdestroy(activity.java:6881)       @ android.app.instrumentation.callactivityondestroy(instrumentation.java:1153)       @ android.app.activitythread.performdestroyactivity(activitythread.java:4190)       @ android.app.activitythread.handledestroyactivity(activitythread.java:4221)        @ android.app.activitythread.-wrap6(activitythread.java)        @ android.app.activitythread$h.handlemessage(activitythread.java:1538)        @ android.os.handler.dispatchmessage(handler.java:102)        @ android.os.looper.loop(looper.java:154)        @ android.app.activitythread.main(activitythread.java:6119)        @ java.lang.reflect.method.invoke(native method)        @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:886)        @ com.android.internal.os.zygoteinit.main(zygoteinit.java:776)  

the lines error occurs follows:

companynavigationactivity.java:533 ‘super.ondestroy();’

@override public void ondestroy() {     getspecificimpofgenericpresenter().closerealm();     super.ondestroy(); } 

genericmvpactivity.java:100 ‘super.ondestroy();’

@override public void ondestroy() {     //unbind butterknife bindings     if(munbinder!=null) {         munbinder.unbind();     }     super.ondestroy(); } 

and goes off android fragment classes. i’m @ loss why foreground service cause issue lifecycle of activity launched in. if comment out call start service, runs normal.

any ideas how service affecting activity in way?

edit 1

i have service listed in manifest as:

<service android:name=".locationservice.foregroundlocationservice"     android:icon="@drawable/bullhorn_24dp"     android:label="assemble sharing"> </service> 

edit 2

presenters closerealm method below, commenting out in activity's ondestroy method gives same error.

public void closerealm() {     this.mrealmdatabase.getinstance().removeallchangelisteners();     this.mrealmdatabase.close(); } 


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -