首页 > 解决方案 > (Firebase Authen)退出并且从 Fragment 到 MainActivity 的意图不起作用

问题描述

class ProfileActivity : AppCompatActivity() {   << on xml file has sign out Button

    var mAuth: FirebaseAuth? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_profile)

        mAuth = FirebaseAuth.getInstance()

        btn_signout.setOnClickListener() {
                mAuth!!.signOut()
                var intent = Intent(this, MainActivity::class.java)
                startActivity(intent)
                finish()
            }


        }
    }



class ChatActivity : AppCompatActivity() {   << activity's using NavbatListener for fragment.

    private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener {item->
        when(item.itemId){
            R.id.nav_chat -> {
                println("home pressed")
                replaceFragment(ChatFragment())
                return@OnNavigationItemSelectedListener  true
            }
            R.id.nav_contact -> {
                println("contact pressed")
                replaceFragment(ContactFragment())
                return@OnNavigationItemSelectedListener  true
            }
            R.id.nav_position -> {
                println("position pressed")
                replaceFragment(PositionFragment())
                return@OnNavigationItemSelectedListener  true
            }
            R.id.nav_profile -> {
                println("profile pressed")
                replaceFragment(ProfileFragment())
                return@OnNavigationItemSelectedListener  true
            }


        }
            false
    }

    private  fun  replaceFragment(fragment: Fragment){
        val fragmentTransaction = supportFragmentManager.beginTransaction()
        fragmentTransaction.replace(R.id.fragmentContainer, fragment)
        fragmentTransaction.commit()
    }



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_chat)

        bottomNavigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
        replaceFragment(ChatFragment())

    }


}

我在 Fragment 上有一个按钮“btn_signout”,我想按意图退出 MainActivity。

如果我在“选项菜单”上使用它可以工作,但我想用一个简单的按钮来做。

我是新的。感谢帮助

标签: androidandroid-fragmentskotlinfirebase-authentication

解决方案


由于您是从片段执行此操作并进行活动,因此您必须这样做:

 btnSignOutAccount.setOnClickListener {
                    AuthUI.getInstance().signOut(this@YourFragmentName.context!!)
                        .addOnCompleteListener {
                            startActivity(intentFor<YourActivityName>().newTask().clearTask())
                        }
                }

上面的 anko 库用于为意图目的精确代码,这里是标准方式的代码:

 val intent = Intent(this@YourFragmentName, YourActivityName::class.java).apply {
                    flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
                }
                startActivity(intent)

只需将其替换为您的 addOnCompleteListener 块的代码即可。
如果您想知道这个标志,它会清除先前活动的状态,这意味着如果您不小心按下它,它不会让您再次回到您的登录活动或您退出的任何活动。


推荐阅读