简体   繁体   中英

Android google: Logout google in another activity

I have use com.google.android.gms.common.SignInButton for create login google app in A. Then i create another activity B use button android custom for logout this connect google. But i can't logout this connect google when i call to static method logout in A activity from activity B. Who have any ideal for my question? I user

private GoogleApiClient mGoogleApiClient;
    private boolean mIntentInProgress;
    private boolean mSignInClicked;
    private ConnectionResult mConnectionResult; 

in A activity. And set OnclickListioner for button logout in Bactivity by call method

public static void signOutFromGplus() {
        if (mGoogleApiClient.isConnected()) {
            Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
            mGoogleApiClient.disconnect();
            Toast.makeText(this, getString(R.string.logout_status) , Toast.LENGTH_LONG).show();
            MyLog.info(getString(R.string.logout_status));
        }
    }

but i give log error: Google api client must connect. Please help me... sorry my bad english

You have to create a Singleton with the GoogleApiClient. Then you can call this object from another activity (B in this case)

GoogleApiClient googleApiClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home); //B Activity

GoogleApiClientHelper googleApiClientHelper = GoogleApiClientHelper.getInstance(getApplicationContext());
googleApiClient = googleApiClientHelper.getGoogleApiClient();
}

And then, you can log out

    private void doLogOut() {
    if ( googleApiClient.isConnected() ) {
        Plus.AccountApi.clearDefaultAccount(googleApiClient);
        googleApiClient.disconnect();
        googleApiClient.connect();

        Intent loginActivity = new Intent(this, LoginActivity.class);
        startActivity(loginActivity);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM