title: java.lang.IllegalStateException: FragmentManager has not been attached to a host
body: <p>I am using MVP and have created a callback which is executed when a button is pressed in sign-in screen and the implementations are in the MainActivity where after executing the callback fragment replaces (not working), I've also put a check.</p>
  <pre><code>if(!isAdded){
     return null
  }
  </code></pre>
  <p>I've already have tried to create new instances for each fragment but still no luck either. I've been stuck for a while please help me out.</p>
  <p><strong>Main Activity</strong></p>
  <pre><code> class MainActivity : AppCompatActivity(), IMainActivityContract.View {
      private lateinit var fragmentManager: FragmentTransaction
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
  
          Constants.mAuth = FirebaseAuth.getInstance()
          changeFragment(Constants.NAV_FRAG_LOGIN)
      }
  
  
      override fun changeFragment(fragmentID: Int) {
          if(supportFragmentManager.isDestroyed || supportFragmentManager.fragments.isEmpty()){
              fragmentManager = supportFragmentManager.beginTransaction()
          }
  
          try {
              when (fragmentID) {
                  Constants.NAV_FRAG_LOGIN -&gt; {
                      fragmentManager.replace(R.id.fragmentContainer,FragmentLogin()).commit()
                  }
  
                  Constants.NAV_FRAG_MAIN -&gt; {
                      fragmentManager.replace(R.id.fragmentContainer, FragmentMain()).commit()
  
                  }
  
                  Constants.NAV_FRAG_ADDITION -&gt; {
                      fragmentManager.replace(R.id.fragmentContainer,FragmentAddition()).commit()
  
                  }
  
                  Constants.NAV_FRAG_CONFIGURATION -&gt; {
                      fragmentManager.replace(R.id.fragmentContainer,FragmentConfiguration()).commit()
  
                  }
              }
          } catch (message: IllegalStateException) {
              Log.d(&quot;LoginDebug&quot;, &quot;Exception is: $message&quot;)
              message.printStackTrace()
          }
      }
  }
  </code></pre>
  <p><strong>Main Activity Contract</strong></p>
  <pre><code>    interface IMainActivityContract {
         interface View{
             fun changeFragment(fragmentID: Int)
         }
  
         interface Presenter{
            //not yet implemented
         }
  } 
  </code></pre>
  <p><strong>Fragment from which callback is getting triggered</strong></p>
  <pre><code>class FragmentLogin : Fragment(), IFragmentLoginContract.View {
          private var fragmentLoginBinding: FragmentLoginBinding? = null
          private lateinit var fragmentLoginPresenter: IFragmentLoginContract.Presenter
          private lateinit var mainActivityView: IMainActivityContract.View
  
      override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
               val view = inflater.inflate(R.layout.fragment_login, container, false)
  
               mainActivityView = MainActivity()
               fragmentLoginPresenter = FragmentLoginPresenter(this)
               fragmentLoginBinding = FragmentLoginBinding.bind(view)
               
                onGmailLogin()
                return view
          }
  
   private fun onGmailLogin() {
          fragmentLoginBinding?.frameLayoutGmailButtonLogin!!.setOnClickListener {
              fragmentLoginPresenter.onGoogleLogin()
  
              mainActivityView.changeFragment(Constants.NAV_FRAG_MAIN)
          }
      }
  }
  </code></pre>
  <p><strong>Error Log</strong></p>
  <blockquote>
  <p>Exception is: java.lang.IllegalStateException: FragmentManager has not been attached to a host.
  2020-08-27 20:19:05.637 8456-8456/me.timezoneplanner W/System.err: java.lang.IllegalStateException: FragmentManager has not been attached to a host.
  2020-08-27 20:19:05.637 8456-8456/me.timezoneplanner W/System.err:     at androidx.fragment.app.FragmentManager.enqueueAction(FragmentManager.java:1739)
  2020-08-27 20:19:05.637 8456-8456/me.timezoneplanner W/System.err:     at androidx.fragment.app.BackStackRecord.commitInternal(BackStackRecord.java:321)
  2020-08-27 20:19:05.637 8456-8456/me.timezoneplanner W/System.err:     at androidx.fragment.app.BackStackRecord.commit(BackStackRecord.java:286)
  2020-08-27 20:19:05.638 8456-8456/me.timezoneplanner W/System.err:     at me.timezoneplanner.activities.MainActivity.changeFragment(MainActivity.kt:41)
  2020-08-27 20:19:05.638 8456-8456/me.timezoneplanner W/System.err:     at me.timezoneplanner.fragments.login.FragmentLogin$onGmailLogin$1.onClick(FragmentLogin.kt:53)</p>
  </blockquote>