【问题标题】:(Firebase Authen ) Sign out and intent from Fragment to MainActivity doesn't work(Firebase Auth)从 Fragment 到 MainActivity 的注销和意图不起作用
【发布时间】:2020-05-01 12:46:09
【问题描述】:
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。

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

我是新人。感谢帮助

【问题讨论】:

  • Activity中的按钮,而不是Fragment中的按钮!

标签: android android-fragments kotlin firebase-authentication


【解决方案1】:

根据您的意图将this 更改为getActivity()。像这样:

   var intent = Intent(getActivity(), MainActivity::class.java) 
   getActivity().startActivity(intent)

【讨论】:

    【解决方案2】:

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

     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 块的代码即可。
    如果您想知道这个标志,它会清除先前活动的状态,这意味着如果您不小心按下它,它不会让您再次回到您的登录活动或您退出的任何活动。

    【讨论】:

      猜你喜欢
      • 2020-08-09
      • 1970-01-01
      • 2015-04-29
      • 1970-01-01
      • 2021-01-22
      • 2021-07-30
      • 2016-08-25
      • 2023-01-20
      相关资源
      最近更新 更多