title: Fatal Exception: java.lang.IllegalStateException: Failure saving state: active fragment was removed from the FragmentManager
body: <p>I recently changed my app to target API Level 28 and also started using androidx instead of support libraries. After the change, I'm noticing a crash that has the following traceback</p>
  
  <pre><code>Fatal Exception: java.lang.IllegalStateException: Failure saving state: active MyFragment{22caf6fc (04a7bbf5-8806-4a45-a25d-616ed244bf18) id=0x7f1000ff} was removed from the FragmentManager
         at androidx.fragment.app.FragmentManagerImpl.saveAllState(FragmentManagerImpl.java:2301)
         at androidx.fragment.app.FragmentController.saveAllState(FragmentController.java:150)
         at androidx.fragment.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:496)
         at androidx.appcompat.app.AppCompatActivity.onSaveInstanceState(AppCompatActivity.java:510)
         at com.company.utils.MyAppCompatActivity.onSaveInstanceState(MyAppCompatActivity.java:161)
         at android.app.Activity.performSaveInstanceState(Activity.java:1311)
         at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1288)
         at android.app.ActivityThread.callCallActivityOnSaveInstanceState(ActivityThread.java:4166)
         at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3577)
         at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3633)
         at android.app.ActivityThread.access$1300(ActivityThread.java:164)
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1491)
         at android.os.Handler.dispatchMessage(Handler.java:102)
         at android.os.Looper.loop(Looper.java:160)
         at android.app.ActivityThread.main(ActivityThread.java:5541)
         at java.lang.reflect.Method.invoke(Method.java)
         at java.lang.reflect.Method.invoke(Method.java:372)
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
  </code></pre>
  
  <p>All my activities are extending from a base activity <code>MyAppCompatActivity</code> which extends from <code>AppCompatActivity</code>. And in the <code>onSaveInstanceState</code> method of <code>MyAppCompatActivity</code>, I have a few log lines there. I have three fragments in this activity implemented under a ViewPager with <code>FragmentStatePageAdapter</code> and the activity itself is not overriding <code>onSaveInstanceState</code> method. </p>
  
  <p>I went through a few SO posts close to this such as <a href="https://stackoverflow.com/questions/34480430/failure-saving-state-active-supportmapfragment-has-cleared-index-1">this one</a> and <a href="https://stackoverflow.com/questions/20984658/failure-saving-activity-state-which-extends-another-activity">this one</a>, but it looks as if it's happening when we're using <code>FragmentManager</code>'s methods to manage the fragments ourselves rather than using a ViewPagerAdapter handle it. I'm not doing that and the closest thing that I'm doing away from the usual flow is overriding <code>FragmentStatePageAdapter</code>'s methods <code>instantiateItem</code> and <code>destroyItem</code> to help me have a method to get a fragment's reference in the code as given in <a href="https://stackoverflow.com/a/15261142/1929309">this SO answer</a>. </p>