title: Android Java - Unable to destroy activity: IndexOutOfBoundsException
body: <p>I am getting the following error whenever I close my application: </p>
  
  <pre><code>FATAL EXCEPTION: main
    Process: uk.cal.codename.projectnedry, PID: 24450
    java.lang.RuntimeException: Unable to destroy activity {uk.cal.codename.projectnedry/uk.CompanyScope.CompanyNavigationActivity.CompanyNavigationActivity}: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
        at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4203)
        at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4221)
        at android.app.ActivityThread.-wrap6(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1538)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
     Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
        at java.util.ArrayList.set(ArrayList.java:427)
        at android.support.v4.app.FragmentManagerImpl.makeInactive(FragmentManager.java:1662)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1452)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
        at android.support.v4.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:2951)
        at android.support.v4.app.FragmentController.dispatchDestroy(FragmentController.java:271)
        at android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:390)
        at android.support.v7.app.AppCompatActivity.onDestroy(AppCompatActivity.java:209)
        at uk.genericmodelviewpresenter.GenericMvpActivity.onDestroy(GenericMvpActivity.java:96)
        at uk.CompanyScope.CompanyNavigationActivity.CompanyNavigationActivity.onDestroy(CompanyNavigationActivity.java:523)
        at android.app.Activity.performDestroy(Activity.java:6881)
        at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1153)
        at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4190)
        at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4221) 
        at android.app.ActivityThread.-wrap6(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1538) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6119) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
  </code></pre>
  
  <p>The only parts that touch code that I have written are:</p>
  
  <p>CompanyNavigationActivity:</p>
  
  <pre><code>@Override
  public void onDestroy() {
      super.onDestroy();
      getSpecificImpOfGenericPresenter().closeRealm();
  }
  </code></pre>
  
  <p>and its superclass GenericMvpActivity:</p>
  
  <pre><code>@Override
  public void onDestroy() {
      super.onDestroy();
      //Unbind Butterknife bindings
      if(mUnbinder!=null) {
          mUnbinder.unbind();
      }
  }
  </code></pre>
  
  <p>From looking at the stacktrace I can see it is something fragment related, but this is within the Android code. This CompanyNavigationActivity does at times contain a number of fragments, <s>but the error occurs whether they are created or not</s>.</p>
  
  <p>Any ideas as to what the cause is?</p>
  
  <p><strong>EDIT 1</strong></p>
  
  <p>The closeRealm method in the presenter class, simply removes listeners and closes the presenters instance of realm.</p>
  
  <pre><code>public void closeRealm() {
      this.mRealmDatabase.getInstance().removeAllChangeListeners();
      this.mRealmDatabase.close();
  }
  </code></pre>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>Upon further testing it seems that if I create fragments held within a viewpager in my CompanyNavigationActivity this error <strong>does not</strong> occur. It seems to occur only when there are no fragments present within the activity.</p>
  
  <p><strong>EDIT 3</strong></p>
  
  <p>I have narrowed it down to a particular method, which is starting a foreground service. More specifically, the startService call.</p>
  
  <pre><code>     /**
       * If foreground location service is not running, start it
       */
      private void startLocationService() {
          if (!ForegroundLocationService.IS_SERVICE_RUNNING) {
              Intent service = new Intent(getSpecificImpOfGenericView().getApplicationContext(), ForegroundLocationService.class);
              service.setAction(ServiceConstants.ACTION.STARTFOREGROUND_ACTION);
              ForegroundLocationService.IS_SERVICE_RUNNING = true;
              getSpecificImpOfGenericView().getApplicationContext().startService(service);
          }
      }
  </code></pre>
  
  <p>Surely a foreground request should remain in the foreground, as a separate service, and not affect the destruction of the activity that started them?</p>