androidkotlinandroid-mvp

java.lang.IllegalStateException: FragmentManager has not been attached to a host


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.

if(!isAdded){
   return null
}

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.

Main Activity

 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 -> {
                    fragmentManager.replace(R.id.fragmentContainer,FragmentLogin()).commit()
                }

                Constants.NAV_FRAG_MAIN -> {
                    fragmentManager.replace(R.id.fragmentContainer, FragmentMain()).commit()

                }

                Constants.NAV_FRAG_ADDITION -> {
                    fragmentManager.replace(R.id.fragmentContainer,FragmentAddition()).commit()

                }

                Constants.NAV_FRAG_CONFIGURATION -> {
                    fragmentManager.replace(R.id.fragmentContainer,FragmentConfiguration()).commit()

                }
            }
        } catch (message: IllegalStateException) {
            Log.d("LoginDebug", "Exception is: $message")
            message.printStackTrace()
        }
    }
}

Main Activity Contract

    interface IMainActivityContract {
       interface View{
           fun changeFragment(fragmentID: Int)
       }

       interface Presenter{
          //not yet implemented
       }
} 

Fragment from which callback is getting triggered

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)
        }
    }
}

Error Log

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)


Solution

  • You should not be initialising mainActivityView with a new instance of MainActivity() rather it should be initialised with its correct parent instance.

    Replace

    mainActivityView = MainActivity()
    

    with

    mainActivityView = (activity as MainActivity)