我正在使用 Firebase pre-built auth UI 和 Jetpack Navigation 组件创建应用。但是,我对如何在我的活动之间导航有点困惑,因为 this.findNavController()
要求我将 view resource id 传递给它(我假设它是我所在的当前视图的 ID? )。如果是这种情况,我将如何获得对 Firebase 预构建身份验证 UI 的引用?还是我误解了它的工作原理?
对于上下文,我试图从 auth 活动导航到 MapsActivity
预先感谢您的帮助!
编辑:
我使用我的 fragmentContainerView
、map
、mapsActivity
和 firebaseAuthUI
的资源 ID 尝试了 this post 中的建议,但没有成功:(>
Navigation.findNavController(this, R.id.mapsActivity).navigate(
FirebaseAuthUIDirections.actionFirebaseAuthUIToMapsActivity(user)
)
这是我的 FirebaseAuthUI 活动的代码(注意 onActivityResult()
中的问题:
class FirebaseAuthUI: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
createSignInIntent()
}
private fun createSignInIntent() {
val providers = arrayListOf(
AuthUI.IdpConfig.GoogleBuilder().build(),
AuthUI.IdpConfig.EmailBuilder().build()
)
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(providers)
.build(),
RC_SIGN_IN
)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == RC_SIGN_IN) {
val response = IdpResponse.fromResultIntent(data)
if (resultCode == Activity.RESULT_OK) {
val user = FirebaseAuth.getInstance().currentUser
this.findNavController().navigate(
FirebaseAuthUIDirections.actionFirebaseAuthUIToMapsActivity(user)
)
} else {
val error = response?.error?.errorCode
Toast.makeText(
this,
"⚠️ Unable to log in. Please contact Support. Error: $error",
Toast.LENGTH_LONG
).show()
}
}
}