简体   繁体   English

Android Firebase Facebook登录会在重新打开应用时显示“退出”按钮

[英]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.

相关问题 确认登录后显示退出按钮,并在使用Facebook Login Integration Android登录时进入活动状态 - Shows Logout Button after confirm login and goes to activity while login using Facebook Login Integration Android Android应用中的Facebook登录/注销状态 - Facebook login/logout status in android app Android:facebook登录和注销 - Android: facebook login & logout Android登录按钮中的Facebook登录集成在登录后更改为注销 - Facebook login intergration in android login button changes to logout after logged in Firebase Facebook 登录即使清除缓存并在卸载应用程序后注销 - Firebase Facebook Login Even If Cleared Cache and Logout after Uninstall App Android应用程序中的Facebook登录按钮 - Facebook Login button in Android app 在Facebook Android SDK 4.0中设置按钮的登录/注销文本? - Set Login/Logout Text for Button in Facebook Android SDK 4.0? 成功登录android facebook应用程序并在新页面上获取注销按钮后如何开始新活动并隐藏登录页面 - How to start a new activity and hide login page after successful login in android facebook app and get logout button on new page 如何在 android 中使用一个注销按钮实现 Facebook 和 Google 登录? - How to implement Facebook and Google login with a one logout button in android? 添加Facebook登录按钮时Android App崩溃 - Android App crashes when adding Facebook login button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM