[英]Android Firebase Facebook Login shows Logout button when app is reopened
public class MainActivity extends AppCompatActivity {
private LoginButton facebookloginButton;
private CallbackManager callbackManager;
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
facebookloginButton=(LoginButton)findViewById(R.id.facebook_login_button);
callbackManager=CallbackManager.Factory.create();
facebookloginButton.setReadPermissions("email", "public_profile");
facebookloginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
handleFacebookAccessToken(loginResult.getAccessToken());
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException error) {
Toast.makeText(getApplicationContext(), "Error",Toast.LENGTH_LONG).show();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
private void handleFacebookAccessToken(AccessToken token) {
AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
FirebaseUser user = mAuth.getCurrentUser();
Intent intent=new Intent(getApplicationContext(),NextActivity.class);
// intent.putExtra("NAME",user.getDisplayName());
startActivity(intent);
finish();
} else {
// If sign in fails, display a message to the user.
Toast.makeText(getApplicationContext(), "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
// ...
}
});
}
@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
// currentUser.getDisplayName();
}
}
I am using Firebase's Facebook Login. 我正在使用Firebase的Facebook登录。 I am able to login and if the login is success I am redirecting to NextActivity.
我能够登录,如果登录成功,我将重定向到NextActivity。
But when I pressed the facebook login button, it shows me the logout button for few seconds and then redirects me to NextActivity. 但是,当我按下facebook登录按钮时,它会向我显示退出按钮几秒钟,然后将我重定向到NextActivity。 When I close my app and open it again, it shows me the MainActivity with Logout button.
当我关闭我的应用并再次打开它时,它会向我显示MainActivity with Logout按钮。
I wanted it to display NextActivity if app is closed and reopened again 如果app关闭并重新打开,我希望它显示NextActivity
You can save the AUTH Token
after the successful login from Facebook in you application's Shared Preferences settings
and then whenever you start your app you can check if there is any token saved in your storage if it means users have already logged in and redirect directly him to next page or profile page. 您可以在应用程序的
Shared Preferences settings
成功登录Facebook后保存AUTH Token
,然后每当您启动应用程序时,您可以检查存储中是否存有任何令牌,如果这意味着用户已经登录并直接重定向到下一页或个人资料页面。
Here is small snippet what you can do: 以下是您可以执行的小片段:
final static String PREFS_NAME = "AUTH"
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
setContentView(R.layout.activity_main);
// check if user is already logged in
// i.e. auth token is present or not
String token = settings.getString("auth_token", null);
// means user is logged in token was found
if (token != null) {
AUTH_TOKEN = token;
startActivity(new Intent(MainActivity.this, ProfileActivity.class));
}
}
// YOUR REST OF THE CODE ....
});
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
FirebaseUser user = mAuth.getCurrentUser();
// SAVE THE USER DETAILS OR PART OF IT IN SHARED PREFS
SharedPreferences.Editor editor = settings.edit();
editor.putString("USER", user);
editor.apply();
Intent intent=new Intent(getApplicationContext(),NextActivity.class);
// intent.putExtra("NAME",user.getDisplayName());
startActivity(intent);
finish();
} else {
// If sign in fails, display a message to the user.
Toast.makeText(getApplicationContext(), "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
// ...
}
this will work since every time you launch your app in onCreate
method checks for that AUTH
file if it's present it will directly send the user to nextActivity
. 这将起作用,因为每次在
onCreate
方法中启动应用程序时检查该AUTH
文件是否存在它将直接将用户发送到nextActivity
。
You can use Shared Preferences for that purpose. 您可以使用共享首选项来实现此目的。
So in the onCreate of your LoginActivity do this: 所以在LoginActivity的onCreate中执行以下操作:
SharedPreferences settings=getSharedPreferences("prefs",0);
boolean firstRun=settings.getBoolean("firstRun",false);
if(firstRun==false)//if running for first time
{
//Set the firstRun value to true
editor.putBoolean("firstRun",true);
editor.commit();
//Continue your login process
}
else
{
// launch NextActivity if app is opened not for the first time
Intent i=new Intent(check.this, NextActivity.class);
startActivity(I);
finish();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.