MGoogleApiClient could not be reached in another action

In my project, I met a problem. I want to use the "Google API" to log in to LoginActivity. And exit another activity (called WelcomeActivity)

LoginActivity: (code here )

public class LoginActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener, View.OnClickListener { // Configuration of Google API - Step 1/3 private static final String TAG = "LoginActivity"; private static final int RC_SIGN_IN = 9001; public static GoogleApiClient mGoogleApiClient; private ProgressDialog mProgressDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); GoogleAPI(); } public void GoogleAPI(){ // Button listeners findViewById(R.id.sign_in_button).setOnClickListener(this); GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestEmail() .build(); mGoogleApiClient = new GoogleApiClient.Builder(this) .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */) .addApi(Auth.GOOGLE_SIGN_IN_API, gso) .build(); } @Override public void onStart() { super.onStart(); .... } // [START onActivityResult] @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...); if (requestCode == RC_SIGN_IN) { GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); handleSignInResult(result); } } // [END onActivityResult] // [START handleSignInResult] private void handleSignInResult(GoogleSignInResult result) { if (result.isSuccess()) { // Signed in successfully, show authenticated UI. CustomApplication app = (CustomApplication)getApplication(); GoogleSignInAccount acct = result.getSignInAccount(); Intent i = new Intent(LoginActivity.this, WelcomePage.class); i.putExtra("Username", acct.getDisplayName()); startActivity(i); } } // [END handleSignInResult] // [START signIn] private void signIn() { Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); startActivityForResult(signInIntent, RC_SIGN_IN); } // [END signIn] @Override public void onConnectionFailed(ConnectionResult connectionResult) { ... } private void showProgressDialog() { ... } private void hideProgressDialog() { ... } @Override public void onClick(View v) { switch (v.getId()) { case R.id.sign_in_button: signIn(); break; …. } } } 

And I want to use the Sign_out Method in my greeting activity,

 private void signOut() { // Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback( // new ResultCallback<Status>() { // @Override // public void onResult(Status status) { // // [START_EXCLUDE] //// updateUI(false); // // [END_EXCLUDE] // } // }); // } 

To solve this problem, I will try 2 methods:

  • make mGoogleApiClient as a global variable (extends the application or Singleton), I try but failed, on the welcome page mGoogleApiClient is not null, but error: mGoogleApiClient is not connected yet .

  • I call LoginActivity.getMGoogleApiClient (static variable) but also failed, same error: mGoogleApiClient is not connected yet .

I have been looking for this problem for several days, but nothing useful to solve it, please help me.

+6
source share
2 answers

When you enable AutoManage, then googleApiClient connects to the onStart system and disables onStop and is automatically processed by the library. What you can do is log out to another activity, no matter what code logs out of your backend, except that you are not logged out of Google.

Pseudocode:

 private void logOut(){ //your logout code in the log out activity setCurrentUser(null); } 

And in action with googleApiClient you can check if a user is registered in the onConnected googleApiClient callback. If the current user is not registered, log out of Google.

 @Override public void onConnected(Bundle connectionHint) { if (getCurrentUser() == null){ signOut(); } } private void signOut() { Auth.GoogleSignInApi.signOut(mGoogleApiClientPlus).setResultCallback( new ResultCallback<Status>() { @Override public void onResult(Status status) { //signed out. } }); } 

If you do not use enableAutoManage and disconnect the client in onStop (which I do not know if it recommended), you can use your 2 methods, but I will not recommend using static fields for the googleApiClient object.

+6
source

One solution is to execute a normal logout thread (ignore mGoogleApiClient). Next time, when the user opens your application on the login screen and deletes the google login, first log out, then google registration

eg:.

 private void googleLogin() { googleLogout(); Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); startActivityForResult(signInIntent, RC_SIGN_IN); } private void googleLogout() { if (mGoogleApiClient.isConnected()) { Auth.GoogleSignInApi.signOut(mGoogleApiClient); } } 
+1
source

All Articles