title: onStop being called after onDestroy?
body: <p>I've encountered a rather strange issue with <code>Activity</code> lifecycle.</p>
  
  <p><strong>Short prequel:</strong></p>
  
  <p>The first symptom that I've found is that it crashed with <code>IllegalArgumentException</code> when I was trying to unregister receiver in <code>onStop</code> after registering it in <code>onStart</code>.</p>
  
  <p>After dumping the full list of active receivers (some reflection magic), I've found that my receiver is not the list. It was either being removed somewhere else by mistake, or it was removed during <code>onDestroy</code> call of <code>Activity</code> where <code>Context</code> is being cleaned (during <code>onDestroy</code> <code>ActivityThread</code> calls <code>ContextImpl#performFinalCleanup</code>, which then calls <code>LoadedApk#removeContextRegistrations</code>).</p>
  
  <p><strong>What is happening?</strong></p>
  
  <p>After adding some more analytics info to the crash, I found out that while the crash is happening in <code>onStop</code>, the <code>Activity</code> is in a pretty weird state - it's <code>isDestroyed()</code> call returns <code>true</code>, it's <code>isFinishing()</code> returns <code>false</code>, and it's <code>getLifecycle().getCurrentState()</code> returns <code>DESTROYED</code>...</p>
  
  <p>Checking normal <code>onStop()</code> (without crash) call shows that the <code>Activity</code> is in this state:</p>
  
  <p><code>isDestroyed()</code> is <code>false</code>, and <code>getLifecycle().getCurrentState()</code> is <code>CREATED</code> in <code>onStop</code>.</p>
  
  <p>So I drew the conclusion that <code>onStop</code> is being called after <code>onDestroy</code>, which I thought is impossible, but it seems to happen.</p>
  
  <p>And <code>onStop</code> is definitely not being called manually by something else in the app, since this is the stack trace of where the <code>onStop</code> is called from.</p>
  
  <pre class="lang-java prettyprint-override"><code>com.myapp.TheActivity.onStop (TheActivity.java:217)
  android.app.Instrumentation.callActivityOnStop (Instrumentation.java:1474)
  android.app.Activity.performStop (Activity.java:8189)
  android.app.ActivityThread.callActivityOnStop (ActivityThread.java:4994)
  android.app.ActivityThread.performStopActivityInner (ActivityThread.java:4967)
  android.app.ActivityThread.handleStopActivity (ActivityThread.java:5047)
  android.app.servertransaction.TransactionExecutor.performLifecycleSequence (TransactionExecutor.java:233)
  android.app.servertransaction.TransactionExecutor.cycleToPath (TransactionExecutor.java:201)
  android.app.servertransaction.TransactionExecutor.executeLifecycleState (TransactionExecutor.java:173)
  android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:97)
  android.app.ActivityThread$H.handleMessage (ActivityThread.java:2220)
  android.os.Handler.dispatchMessage (Handler.java:107)
  android.os.Looper.loop (Looper.java:237)
  android.app.ActivityThread.main (ActivityThread.java:8016)
  java.lang.reflect.Method.invoke (Method.java)
  com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:493)
  com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1076)
  </code></pre>