title: DialogFragment IllegalStateException when screen goes off
body: <p>First of all, I've read dozens of SO questions and read excellent post by Alex Lockwood about the <code>IllegalStateException</code> problems connected with fragment transactions: <a href="https://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html" rel="nofollow noreferrer">https://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html</a> - <strong>tl;dr I've done my homework</strong></p>
  
  <p>But what I didn't find anywhere is the <code>IllegalStateException</code> problem when the screen goes off and we are trying to commit fragment transaction - show <code>DialogFragment</code> in my case.</p>
  
  <p>What is exactly happening in my app is reacting to other activity result and later on showing a <code>DialogFragment</code>. <strong>As suggested by many SO users, I'm not showing dialog from <code>onActivityResult</code> but from <code>onPostResume</code> (I've tried also with <code>onResumeFragments</code>)</strong> Simplified code looks like this</p>
  
  <pre><code>override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
      super.onActivityResult(requestCode, resultCode, data)
      Timber.i("${this::class.java.simpleName} onActivityResult")
      activityResult = ActivityResult(this::class, requestCode, resultCode, data)
  }
  
  override fun onPostResume() {
      super.onPostResume()
      Timber.i("${this::class.java.simpleName} onPostResume")
      activityResult?.let { ActivityResultRepository.resultsSubject.onNext(it) }
  }
  
  override fun onResumeFragments() {
      super.onResumeFragments()
      Timber.i("${this::class.java.simpleName} onResumeFragments")
  }
  </code></pre>
  
  <p>I couldn't reproduce exception on my device (Mate 20 pro) or on emulator but I'm receiving crash reports from users of my app (0.5% users affected)</p>
  
  <p>My assumption is, my app is trying to show a dialog after screen goes off. Screen going off is an arbitrary event that causes <code>onSaveInstanceState</code> method to be called and it can be called right before my <code>onPostResume</code> method. <strong>However this is only my assumption!, maybe there is another cause of the exception</strong></p>
  
  <p>Has anyone experienced similar problems and manage to handle them? I know I can use <code>commitAllowingStateLoss</code> but firstly - <code>show</code> method of <code>DialogFragment</code> doesn't allow to do that, would have to show dialog manually by using <code>FragmentManager</code> (don't know if there are any implications), secondly <code>commitAllowingStateLoss</code> is generally a bad thing to do</p>
  
  <p>I'm adding crash report although everyone saw it already somewhere :)</p>
  
  <pre><code>Caused by java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
         at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:2053)
         at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:2079)
         at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:678)
         at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:632)
         at android.support.v4.app.DialogFragment.show(DialogFragment.java:143)
  </code></pre>