Google+ sign out from a different activity Google+ sign out from a different activity android android

Google+ sign out from a different activity


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

@Overrideprotected 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:

 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);                      }                  });      }  });


Hey i solved this problem by myself, working like charm

What is the problem : Google plus signIn in one activity but need to Logout from another activity

Solution:

My Google-plus Logout Activity is like this:

public class MainActivity extends Activity implements OnClickListener,    ConnectionCallbacks, OnConnectionFailedListener,    ResultCallback<People.LoadPeopleResult> {   GoogleApiClient mGoogleApiClient;   boolean mSignInClicked;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    mGoogleApiClient = new GoogleApiClient.Builder(this)            .addConnectionCallbacks(this)            .addOnConnectionFailedListener(this).addApi(Plus.API)            .addScope(Plus.SCOPE_PLUS_LOGIN).build();     //copy this code on "Logout" Onclick  logout.setOnClickListener(new OnClickListener() {        @Override        public void onClick(View v) {            // TODO Auto-generated method stub             if (mGoogleApiClient.isConnected()) {            Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);            mGoogleApiClient.disconnect();            mGoogleApiClient.connect();            // updateUI(false);            System.err.println("LOG OUT ^^^^^^^^^^^^^^^^^^^^ SUCESS");        }         }    });}@Overridepublic void onConnected(Bundle arg0) {    // TODO Auto-generated method stub    mSignInClicked = false;    // updateUI(true);    Plus.PeopleApi.loadVisible(mGoogleApiClient, null).setResultCallback(            this);}@Overridepublic void onConnectionSuspended(int arg0) {    // TODO Auto-generated method stub    mGoogleApiClient.connect();    // updateUI(false);}@Overridepublic void onConnectionFailed(ConnectionResult arg0) {    // TODO Auto-generated method stub}protected void onStart() {    super.onStart();    mGoogleApiClient.connect();}protected void onStop() {    super.onStop();    if (mGoogleApiClient.isConnected()) {        mGoogleApiClient.disconnect();    }}@Overridepublic void onResult(LoadPeopleResult arg0) {    // TODO Auto-generated method stub}

Description about solution:

For single package google plus API will generate one token & session.Just here simply make one more session in logout page also.You can easily logout from session now

I to have tried a lot about this problem,to logout from current session, just try this .it will definitely work. any doubts let me know


It would probably be easier to create a base class and inherit the connect/disconnect methods. Photohunt, our full sample, documents this design in detail.

DocsCode