Sign out from google and facebook in android application

Jarvis picture Jarvis · Jan 13, 2015 · Viewed 30.8k times · Source

I have integrated google and facebook sign up using their respective methods. But after successful signing, I want to open different activity and similarly user can go to various different activities. I have used action bar where I am giving an option to sign out from either of the account that the user has logged in. How could I sign out the user when I am in different activity then the main activity. I am getting an exception and I am not able to pass apiclient reference (for google) and session reference (for facebook) to another activity. Please help. Thanks in advance.

Answer

sommesh picture sommesh · Aug 16, 2016

Log out from google:

Just add this on your new activity, where you want your logout-button for google+ to be there :

@Override
protected void onStart() {
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();
    mGoogleApiClient.connect();
    super.onStart();
}

and next you can set setOnClickListener on button:

signout.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
      Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
              new ResultCallback<Status>() {
                  @Override
                  public void onResult(Status status) {
                      // ...
                      Toast.makeText(getApplicationContext(),"Logged Out",Toast.LENGTH_SHORT).show();
                      Intent i=new Intent(getApplicationContext(),MainActivity.class);
                      startActivity(i);
                  }
              });
  }
});