0

I click on SignOut but I don't know if it logs out or not because I stay in the same fragment."I want to go to the SignInActivity page when I log out, but it doesn't go and stays in the same fragment. That's my problem." What could be the reason?"

HomePageActivity.kt

class HomePageActivity : AppCompatActivity() {

private lateinit var auth : FirebaseAuth
//Bottom Navigation View
val homeFragment = HomeFragment()
val myAccountFragment = MyAccountFragment()
val notificationFragment = NotificationFragment()

override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_home_page)

    auth = FirebaseAuth.getInstance()

    //Bottom Navigation View
    val bottomNavigationView: BottomNavigationView = findViewById(R.id.bottom_navigation)

    supportFragmentManager.beginTransaction().replace(R.id.container, HomeFragment()).commit()

    bottomNavigationView.setOnItemSelectedListener { item ->
        when (item.itemId) {
            R.id.home -> {
                supportFragmentManager.beginTransaction().replace(R.id.container, homeFragment)
                    .commit()
                true
            }
            R.id.notification -> {
                supportFragmentManager.beginTransaction()
                    .replace(R.id.container, notificationFragment).commit()
                true
            }
            R.id.my_account -> {
                supportFragmentManager.beginTransaction()
                    .replace(R.id.container, myAccountFragment).commit()
                true
            }
            else -> false
        }
    }
}

}

MyAccountFragment.kt

class MyAccountFragment : Fragment() {

private lateinit var auth: FirebaseAuth

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    auth = FirebaseAuth.getInstance()
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val view = inflater.inflate(R.layout.fragment_my_account, container, false)

    setHasOptionsMenu(true)

    val toolbar: Toolbar = view.findViewById(R.id.my_account_toolbar)

    toolbar.inflateMenu(R.menu.my_account_fragment_menu)

    return view
}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
    inflater.inflate(R.menu.my_account_fragment_menu, menu)
    super.onCreateOptionsMenu(menu, inflater)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    if (item.itemId == R.id.SignOut) {
        auth.signOut()
        val intent = Intent(activity, SignInActivity::class.java)
        intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
        startActivity(intent)
        activity?.finish()
        return true
    }
    return super.onOptionsItemSelected(item)
}

}

"I click on SignOut but I don't know if it logs out or not because I stay in the same fragment."I want to go to the SignInActivity page when I log out, but it doesn't go and stays in the same fragment. That's my problem." What could be the reason?"

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193

1 Answers1

0

I click on SignOut but I don't know if it logs out or not because I stay in the same fragment.

That's basically because you're only signing out, without knowing when it really happens. To solve that, you have to attach an AuthStateListener, so you can track when the state changes. You can simply achieve that, using the solution in my answer from the following post:

If you want however to use a LiveData object, then the answer from the following post will also help:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193