Cannot Cancel Google+ Sign In Button

I am new to Android development and hope to get some recommendations on the problem I am facing.

My application requires a google + button from me.

My progress

  • I followed the recommendations and completed all the necessary configuration steps for the google + login button
  • I can use the login button and receive the profile email.

My problem

  • When you click the Google + login button, the "choose an account" dialog box appears, which allows the user to choose from several possible gmail accounts.
  • When the user clicks on the account and then presses the confirmation button, everything works fine
  • But , when the user clicks the cancel button in the dialog box, the dialog disappears and reappears. Even if you press the back button, the dialog will disappear and reappear.

This does not allow the user to select other login parameters.

I am wondering what is wrong with my code, any help would be appreciated. Thanks.

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // facebook sign in FacebookSdk.sdkInitialize(getApplicationContext()); setContentView(R.layout.activity_sign_in); facebookLoginSetup(findViewById(android.R.id.content).getRootView()); mGoogleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(Plus.API) .addScope(Plus.SCOPE_PLUS_LOGIN) .addScope(Plus.SCOPE_PLUS_PROFILE) .build(); SignInButton sign_in_button = (SignInButton) findViewById(R.id.sign_in_button); setGooglePlusButtonText(sign_in_button, getString(R.string.google_login_button_label)); findViewById(R.id.sign_in_button).setOnClickListener(this); mProgressDialog = new ProgressDialog(this); mProgressDialog.setMessage(getString(R.string.global_message_loading)); mProgressDialog.setCancelable(false); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(mProgressDialog.isShowing()){ mProgressDialog.dismiss(); } // google if (requestCode == RC_SIGN_IN) { if (resultCode != RESULT_OK) { mSignInClicked = false; } mIntentInProgress = false; if (!mGoogleApiClient.isConnecting()) { mGoogleApiClient.reconnect(); } } else { // facebook // call registered call back method callbackManager.onActivityResult(requestCode, resultCode, data); } } @Override public void onConnectionSuspended(int cause) { mGoogleApiClient.connect(); } @Override public void onClick(View v) { if (v.getId() == R.id.sign_in_button && !mGoogleApiClient.isConnecting()) { if(!mProgressDialog.isShowing()){ mProgressDialog.show(); } mSignInClicked = true; mGoogleApiClient.connect(); } } @Override public void onConnected(Bundle connectionHint) { mSignInClicked = false; if(mProgressDialog.isShowing()){ mProgressDialog.dismiss(); } if (Plus.AccountApi.getAccountName(mGoogleApiClient) != null) { String userEmail = Plus.AccountApi.getAccountName(mGoogleApiClient); createUser(userEmail); } } @Override public void onConnectionFailed(ConnectionResult result) { if (!mIntentInProgress && result.hasResolution()) { try { Log.d(MainActivity.TAG, "onConnectionFailed keep retrying"); mIntentInProgress = true; startIntentSenderForResult(result.getResolution().getIntentSender(), RC_SIGN_IN, null, 0, 0, 0); } catch (IntentSender.SendIntentException e) { // The intent was canceled before it was sent. Return to the default // state and attempt to connect to get an updated ConnectionResult. mIntentInProgress = false; } } } // google custom button protected void setGooglePlusButtonText(SignInButton signInButton, String buttonText) { for (int i = 0; i < signInButton.getChildCount(); i++) { View v = signInButton.getChildAt(i); if (v instanceof TextView) { TextView tv = (TextView) v; tv.setTextSize(15); tv.setTypeface(null, Typeface.NORMAL); tv.setInputType(InputType.TYPE_TEXT_FLAG_CAP_WORDS); tv.setText(buttonText); return; } } } 
+5
source share
3 answers

In onActivityResult check the resultCode method.

 if(responseCode == RESULT_OK){ //User clicked sign in do your stuff }else if(responseCode == RESULT_CANCELED){ //User clicked cancel mIntentInProgress = true; } //if (!mIntentInProgress && result.hasResolution()) { 

Now onConnectionFailed does not execute startIntentSenderForResult

0
source

I have the same problem too, solved with this

 SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); SharedPreferences.Editor editor = sp.edit(); editor.putString(PREF_ACCOUNT_NAME, "cancel"); editor.apply(); this.finish(); 

Thanks...

0
source

I had the same problem. For the cancel action (responseCode! = RESULT_OK) it returns true, so I will catch it in the onActivityResult (..) method. Here is the code:

  protected void onActivityResult(int requestCode, int responseCode, Intent data) { // TODO Auto-generated method stub if (requestCode == RC_SIGN_IN) { if (responseCode != RESULT_OK) { mSignInClicked = false; }else if (responseCode == RESULT_OK) { if (!mGoogleApiClient.isConnected()) { mGoogleApiClient.reconnect(); } } mIntentInProgress = false; } else { ParseFacebookUtils.onActivityResult(requestCode, responseCode, data); } } 
0
source

All Articles