gpt4 book ai didi

android - 当用户从 Firebase 注销时显示登录

转载 作者:行者123 更新时间:2023-11-30 04:57:36 25 4
gpt4 key购买 nike

在我的 fragment 中我有对话框(注销),当用户点击他从应用程序注销时:

override fun onClick(dialog: DialogInterface?, which: Int) {
AuthUI.getInstance().signOut(requireContext())
}

但在那之后,我想将用户发送到授权 fragment ,在我的例子中是 MainFragment

所以我在我的 nav_graph 中添加了下一个代码:

<fragment
android:id="@+id/settings_list_fragment"
android:name="com.mandarine.targetList.features.settings.SettingsListFragment"
android:label="@string/settings"
tools:layout="@layout/fragment_settings_list" >
<action
android:id="@+id/action_settings_list_fragment_to_mainFragment"
app:destination="@id/mainFragment" />
</fragment>

还在我的函数 onClick() 中添加一行:

override fun onClick(dialog: DialogInterface?, which: Int) {
AuthUI.getInstance().signOut(requireContext())
findNavController().navigate(R.id.action_settings_list_fragment_to_mainFragment)
}

但总是当我移动到 MainFragment 时,我的用户 不是 null

只有当我终止应用程序时,用户才为 null

这是我的 MainFragment 的代码:

class MainFragment : Fragment() {

private lateinit var auth: FirebaseAuth
private lateinit var mAuthStateListener: FirebaseAuth.AuthStateListener

companion object {
const val TAG = "MainFragment"
const val SIGN_IN_RESULT_CODE = 1001
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_main, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
signIn()
launchSignInFlow()
}

override fun onResume() {
super.onResume()
auth.addAuthStateListener(mAuthStateListener)
}

override fun onPause() {
super.onPause()
auth.removeAuthStateListener(mAuthStateListener)
}

private fun signIn() {
auth = FirebaseAuth.getInstance()
mAuthStateListener = FirebaseAuth.AuthStateListener { firebaseAuth ->
if (firebaseAuth.currentUser != null) {
Log.d("some", "user not null")
findNavController().navigate(R.id.show_goals)
} else {
Log.d("some", "null")
}
}
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == SIGN_IN_RESULT_CODE) {
val response = IdpResponse.fromResultIntent(data)
if (resultCode == Activity.RESULT_OK) {
// User successfully signed in
Log.i(TAG, "Successfully signed in user ${FirebaseAuth.getInstance().currentUser?.displayName}!")
} else {
// Sign in failed. If response is null the user canceled the
// sign-in flow using the back button. Otherwise check
// response.getError().getErrorCode() and handle the error.
Log.i(TAG, "Sign in unsuccessful ${response?.error?.errorCode}")
}
}
}

private fun launchSignInFlow() {
// Give users the option to sign in / register with their email
// If users choose to register with their email,
// they will need to create a password as well
val providers = arrayListOf(
AuthUI.IdpConfig.EmailBuilder().build(),
AuthUI.IdpConfig.GoogleBuilder().build()
)

// Create and launch sign-in intent.
// We listen to the response of this activity with the
// SIGN_IN_RESULT_CODE code
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(providers)
.build(),
SIGN_IN_RESULT_CODE
)
}
}

更新:例如,如果我在 SettingsListFragment 中更改我的 onClick,所有工作都是正确的:

override fun onClick(dialog: DialogInterface?, which: Int) {
AuthUI.getInstance().signOut(requireContext())
activity?.finish()
}

但我不希望用户退出我的应用

最佳答案

修改了我的nav_graph.xml:

<fragment
android:id="@+id/settings_list_fragment"
android:name="com.mandarine.targetList.features.settings.SettingsListFragment"
android:label="@string/settings"
tools:layout="@layout/fragment_settings_list">

<action
android:id="@+id/sign_in"
app:destination="@id/mainFragment"/>
</fragment>

并添加下一个代码:

override fun onClick(dialog: DialogInterface?, which: Int) {
activity?.let {
AuthUI.getInstance().signOut(it).addOnCompleteListener {
findNavController().navigate(R.id.sign_in)
}
}
}

这解决了我的问题。

关于android - 当用户从 Firebase 注销时显示登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58856736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com