Google+ sign out from a different activity

前端 未结 9 1658
终归单人心
终归单人心 2020-12-05 00:17

I have started using the Google+ API for android, and I have created a sign-in application following this tutorial:

https://developers.google.com/+/mobi

相关标签:
9条回答
  • 2020-12-05 00:54

    You can get instance of FirebaseAuth anywhere from the app as FirebaseAuth is a singleton class.

     mAuth = FirebaseAuth.getInstance();
     mAuth.signOut();
    
    0 讨论(0)
  • 2020-12-05 00:55

    Here's my solution. I have made a Utils singleton class. In my LoginActivity, I have a GoogleSignInClient object. So just before starting the DashboardActivity after login, I save the instance of googleSignInClient object by calling Utils.getInstance().setGoogleSignInClient(googleSignInClient). Now anywhere else, if I want to logout I have this method in Utils ready:

    public void signOut() {
        googleSignInClient.signOut();
        FirebaseAuth.getInstance().signOut();
    }
    

    So now, I can do this from any other activity:

    else if (id == R.id.action_logout) {
        Utils.getInstance().signOut();
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
    }
    

    Yes, you need to log out from both of them, otherwise, you might not see the account chooser the next time you tap the login button.

    0 讨论(0)
  • 2020-12-05 00:56

    sommesh's answer is perfect, but for less code you can use "Public Static Method" like this:

    public static GoogleApiClient mGoogleApiClient;
    ...
    ...
    public static void signOutFromGoogle() {
        Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
                new ResultCallback<Status>() {
                    @Override
                    public void onResult(Status status) {
                        //...
                    }
                });
    }
    

    And on your other Activity call it:

    Your_Google_Activity.mGoogleApiClient.connect();
    btnSignOut.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {            
            Your_Google_Activity.signOutFromGoogle();
        }
    });
    
    0 讨论(0)
提交回复
热议问题