0

I have FragmentActivity and there are 4 fragments inside. LoginFragment, RegistrationFragment, ForgottenPasswordFragment, LocalityFragment.

User can switch between them by back button, but if you push back button in default fragment (LoginFragment or LocalityFragment) I want to disable it, because it will return user to the SplashScreen.

So I've found some method how to get currently displayed fragment. I've tried fragment.userVisibleHint and fragment.isVisible. Both of them are returning false as I push back button even if that fragment is currently displayed on screen.

Code:

override fun onBackPressed() {
        val activeLoginFragment = supportFragmentManager.findFragmentByTag("LOGIN_FRAGMENT") as LoginFragment?
        val activeLocalityFragment = supportFragmentManager.findFragmentByTag("LOCALITY_FRAGMENT") as LocalityPickFragment?

        createLog("onBackPressedLogin ", activeLoginFragment.toString())
        createLog("onBackPressedLogin ", "LoginFragment is: " + activeLoginFragment?.userVisibleHint)
        if ( (activeLoginFragment == null || !activeLoginFragment.userVisibleHint) || (activeLocalityFragment == null || !activeLocalityFragment.userVisibleHint) ){
            createLog("onBackPressedLogin ", "not visible")
            super.onBackPressed()
        }

    }

fun switchToForgottenPasswordFragment(){
        val mPendingRunnable = Runnable {
            fragmentTransaction = supportFragmentManager.beginTransaction()
            fragmentTransaction.apply {
                replace(R.id.startup_fragment_container, forgottenPasswordFragment)
                addToBackStack(null)
                commit()
            }
        }

        val fragmentThread = Thread(mPendingRunnable)
        fragmentThread.start()
    }
martin1337
  • 2,384
  • 6
  • 38
  • 85
  • 2
    use `findFragmentById` method with your fragment container id and check for it's instance. – Jeel Vankhede Oct 27 '18 at 10:30
  • And how do I know each fragment's ID? Or instance of FragmentClassName? – martin1337 Oct 27 '18 at 10:33
  • How are you inflating your fragments? – Jeel Vankhede Oct 27 '18 at 10:34
  • I create instance of each fragment in onCreate and then I just switching these instances by function. (Added to my post). – martin1337 Oct 27 '18 at 10:37
  • I mean are you using `FrameLayout` to inflate dynamically or using `fragment` tag in xml? – Jeel Vankhede Oct 27 '18 at 10:39
  • Yep I have frameLayout (startup_fragment_container) in function – martin1337 Oct 27 '18 at 10:41
  • [Check this](https://stackoverflow.com/a/36646615/5647037) Basically the same as @JeelVankhede said already. – Vucko Oct 27 '18 at 10:42
  • So I should do something like this: val activeFragment = supportFragmentManager.findFragmentById(R.id.startup_fragment_container) if activeFragment is LoginFragment: do something – martin1337 Oct 27 '18 at 10:42
  • 1
    Yes, exactly like that, check if the fragment is an instance of the type you care about. Also an unrelated note, you should FINISH your splash screen, so it's not even possible for the user to get to it. It's kinda bad if they do in any way. – Vucko Oct 27 '18 at 10:43
  • Could you provide the code you use to add the fragments? – tynn Oct 27 '18 at 15:50

0 Answers0