android - (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。
如果我在“选项菜单”上使用它可以工作,但我想用一个简单的按钮来做。
我是新的。感谢帮助
解决方案
由于您是从片段执行此操作并进行活动,因此您必须这样做:
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 块的代码即可。
如果您想知道这个标志,它会清除先前活动的状态,这意味着如果您不小心按下它,它不会让您再次回到您的登录活动或您退出的任何活动。
推荐阅读
- php - LAST_INSERT_ID() 无法使此代码工作
- windows - 在 Windows 7 Enterprise 下禁用 Powershell 2.0
- ms-word - 创建一个 VSTO Word 插件,然后按下链接按钮打开文件
- c++ - char 指针或 char 变量的默认值是多少
- maven - 使用 Accessmbly 插件构建 Maven 失败
- javascript - 如何使用 fast-csv npm 将新行或新行的数据(新行)附加到现有的 csv 文件
- java - 请求获取参数(
) 返回空值 - javascript - Asp.Net Java 脚本根据循环后面的代码更改 Gridview 行颜色
- optaplanner - 将医院病床规划 (PAS - 患者入院安排)) 与我的网络应用程序集成
- python - 如何注释类方法返回该类的实例