[英]Android Facebook Login. Prevent button from changing to "log out"
Let me describe the application im trying to do using Kotlin, on Android Studio.让我描述一下我尝试使用 Kotlin 在 Android Studio 上做的应用程序。
Splash Screen => Login Screen => Main App初始屏幕 => 登录屏幕 => 主应用程序
Splash Screen: Just a photo Login Screen: Provides different ways of logging (Google, Facebook, etc) MainActivity: Allows you to log off, in that case, you must return to "Login Screen" Splash Screen: Just a photo Login Screen: Provides different ways of logging (Google, Facebook, etc) MainActivity:允许您注销,在这种情况下,您必须返回“登录屏幕”
So far I have been working only with Facebook Login.到目前为止,我一直只使用 Facebook 登录。 I managed to place the button, make it work, and get a proper uid.
我设法放置了按钮,使其工作,并获得了正确的 uid。 The trouble is that, when that button is clicked, it automatically switches to "Log Out".
问题在于,单击该按钮时,它会自动切换到“注销”。 So, when I go back from my MainActivity to my Login Screen, instead of having the button to Login again, im having a "Log Out" button, when account is actually already logged out.
所以,当我 go 从我的 MainActivity 返回到我的登录屏幕时,我没有再次登录的按钮,而是有一个“注销”按钮,当帐户实际上已经注销时。
Is there a way to prevent this button from changing?有没有办法防止这个按钮改变? I have been reading tons of documentation, but havent found anything useful.
我一直在阅读大量文档,但没有发现任何有用的东西。 Is my idea incorrect?
我的想法不正确吗? Or is there a better way to do this?
或者有更好的方法吗? Note that everytime I Leave the LoginScreen, I place a finish().
请注意,每次离开登录屏幕时,我都会放置一个 finish()。 The reason of this was to try to reset the activity, and make it work as if the program was running from scratch.
这样做的原因是试图重置活动,并使其像程序从头开始运行一样工作。
Variables defined on LoginScreen LoginScreen 上定义的变量
private var mCallBackManager : CallbackManager?= null
private var mFirebaseAuth : FirebaseAuth?= null
On create function在创建 function
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.lay_login_screen)
//-----------Inicializadores-Facebook---------------
mFirebaseAuth = FirebaseAuth.getInstance()
FacebookSdk.sdkInitialize(getApplicationContext())
mCallBackManager = CallbackManager.Factory.create()
//--------------------------------------------------
//-----------Boton-Facebook-------------------------
Button_LoginScreen_LoginFace.setOnClickListener()
{
iniciarSesionFacebook();
}
//--------------------------------------------------
}
private fun iniciarSesionFacebook()
{
Button_LoginScreen_LoginFace.registerCallback(mCallBackManager, object : FacebookCallback<LoginResult>
{
override fun onSuccess(result: LoginResult?)
{
d(getString(R.string.TAG_FacebookLogin),"Login Correct")
handleFacebookToken(result!!.accessToken)
}
override fun onCancel()
{
d(getString(R.string.TAG_FacebookLogin),"Login cancelled")
}
override fun onError(error: FacebookException?)
{
d(getString(R.string.TAG_FacebookLogin),"Login Error")
}
})
}
private fun handleFacebookToken(accessToken: AccessToken?)
{
val credential = FacebookAuthProvider.getCredential(accessToken!!.token)
mFirebaseAuth!!.signInWithCredential(credential).addOnFailureListener()
{error->
d(getString(R.string.TAG_FacebookLogin),"Error 1"+error.message)
}
.addOnSuccessListener { resultado->
startActivity(Intent(this, MainActivity::class.java))
finish()
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?)
{
super.onActivityResult(requestCode, resultCode, data)
mCallBackManager!!.onActivityResult(requestCode,resultCode,data)
}
Finally, this is how I "Log Out" from MainActivity, and return to LoginScreen最后,这就是我从 MainActivity“注销”并返回到 LoginScreen 的方式
class MainActivity : AppCompatActivity()
{
//----------Variables-Globales----------------
private var mFirebaseAuth : FirebaseAuth?= null
//--------------------------------------------
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//-------Inicializo-Variables------------------------
mFirebaseAuth = FirebaseAuth.getInstance()
//---------------------------------------------------
Button_MainActity_LogOf.setOnClickListener{
val firebaseaux=mFirebaseAuth
firebaseaux?.signOut()
startActivity(Intent(this, ActivityLoginScreen::class.java))
}
}
}
This is how the LoginScreen looks the first time is ran.这是 LoginScreen 第一次运行时的样子。
This is how it looks after manually logging of.这是手动登录后的样子。 Note that is not only the appearence of the code, the code itself changes.
请注意,不仅是代码的外观,代码本身也会发生变化。 Now the button doesnt allow you to LogIn.
现在该按钮不允许您登录。
Just to add some extra information, i have found this property on the docs只是为了添加一些额外的信息,我在文档中找到了这个属性
Configuration: auto_logout_link配置: auto_logout_link
HTML5 Attribute: data-auto-logout-link HTML5 属性: data-auto-logout-link
Description: If its activated, the button will be replaced by a LogOut button if the user has already logged in说明:如果激活,如果用户已经登录,则该按钮将被注销按钮取代
Options: True,False选项:真、假
This es EXACTLY what im looking for.这正是我要找的。 But, from what I can see, that is only ment for webpages and not android. Does somebody know how to touch this configuration in android?
但是,据我所知,这只是针对网页的说明,而不是 android。有人知道如何在 android 中触摸此配置吗? or the equivalent adroid option?
或等效的机器人选项? I will add the link where I found the property.
我将添加我找到该属性的链接。 https://developers.facebook.com/docs/facebook-login/web/login-button/
https://developers.facebook.com/docs/facebook-login/web/login-button/
Thanks in advance提前致谢
Ok, after some time I managed to solve the Issue.好的,一段时间后我设法解决了这个问题。 I was logging out from the Firebase Authentication, but not from Facebook authentication.
我从 Firebase 身份验证注销,但不是从 Facebook 身份验证注销。 I didnt manage to stop the button from behaving like it does, but at least the behaviour now is correct.
我没有设法阻止按钮的行为,但至少现在的行为是正确的。 I will share my new LogOut code.
我将分享我的新注销代码。
Button_MainActity_LogOf.setOnClickListener{
FirebaseAuth.getInstance().signOut() //Log out from Firebase
if(isFacebookLogin()) //Check if logged in on facebook
{
LoginManager.getInstance().logOut() //Log out from facebook
}
//Here i should log out from other providers
startActivity(Intent(this, ActivityLoginScreen::class.java))
}
private fun isFacebookLogin(): Boolean
{
return AccessToken.getCurrentAccessToken() !=null
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.