title: Androidx Navigation IllegalStateException after onSaveInstanceState
body: <p>I have an app using AndroidX's Navigation library, but I'm getting odd behavior. Particularly around my app going in/out of the background. Here are two examples:</p>
  
  <ol>
  <li>In a simple on click listener in a Fragment I have:</li>
  </ol>
  
  <p>(Kotlin)</p>
  
  <pre><code>button.setOnClickListener {
      findNavController().popBackStack()
  }
  </code></pre>
  
  <p>From this, I see crashes saying it threw an <code>IllegalStateException</code> since it ran after <code>onSaveInstanceState</code>.</p>
  
  <ol start="2">
  <li>I have a <code>ViewModel</code> associated with my <code>Fragment</code> and I register my observers to the fragment view's lifecycle. This means that I get notified during <code>onStart</code>. Some key events, such as login state determine the app's navigation. In my case I have a splash screen that could go to either a login screen or the main screen. Once a user completes login, I reset the navigation (taking me back to the splash screen). Now the auth state is ready and I want to navigate to the main fragment, this throws an error often because <code>onResume</code> must be called before the <code>FragmentManager</code> is considered ready. I get an error saying I'm in the middle of a transaction and I can't add a new one. To mediate this I had to write this strange bit of code:</li>
  </ol>
  
  <p>(Kotlin)</p>
  
  <pre><code>private fun safeNavigateToMain() {
      if (fragmentManager == null) {
        return
      }
      if (!isResumed) {
          view?.post { safeNavigateToMain() }
          return
      }
      try {
          findNavController().navigate(R.id.main)
      } catch (tr: Throwable) {
          view?.post { safeNavigateToMain() }
      }
  }
  </code></pre>
  
  <p>Does anyone know how I can get the navigation controller to play nice with the fragment lifecycles without having to add these workarounds?</p>