Friday, 15 June 2012

Android Play Services Login: Google Plus cancel button is not working properly -



Android Play Services Login: Google Plus cancel button is not working properly -

i trying implement google+ authentication in android application. in order this, have followed this google tutorial.

when permission dialog appears, if user clicks sign in, works fine. however, if clicks cancel, dialog closes couple of seconds , shows up. goes on forever there's no way cancel operation. why happening?

this relevant code, adapted tutorial:

/* request code used invoke sign in user interactions. */ private static final int rc_sign_in = 0; /* client used interact google apis. */ private googleapiclient mgoogleapiclient; /* track whether sign-in button has been clicked know resolve * issues preventing sign-in without waiting. */ private boolean msigninclicked; /* store connection result onconnectionfailed callbacks can * resolve them when user clicks sign-in. */ private connectionresult mconnectionresult; /* flag indicating pendingintent in progress , prevents * starting farther intents. */ private boolean mintentinprogress; protected void onstart() { super.onstart(); mgoogleapiclient.connect(); } protected void onstop() { super.onstop(); if (mgoogleapiclient.isconnected()) { mgoogleapiclient.disconnect(); } } /* helper method resolve current connectionresult error. */ private void resolvesigninerror() { if (mconnectionresult.hasresolution()) { seek { mintentinprogress = true; startintentsenderforresult(mconnectionresult.getresolution().getintentsender(), rc_sign_in, null, 0, 0, 0); } grab (intentsender.sendintentexception e) { // intent canceled before sent. homecoming default // state , effort connect updated connectionresult. mintentinprogress = false; mgoogleapiclient.connect(); } } } public void onconnectionfailed(connectionresult result) { if (!mintentinprogress) { // store connectionresult can utilize later when user clicks // 'sign-in'. mconnectionresult = result; if (msigninclicked) { // user has clicked 'sign-in' effort resolve // errors until user signed in, or cancel. resolvesigninerror(); } } } // login email button click listener. public class buttonlogingplusclicked implements view.onclicklistener { @override public void onclick(view view) { if (view.getid() == r.id.sign_in_button && !mgoogleapiclient.isconnecting()) { msigninclicked = true; resolvesigninerror(); } } } @override public void onconnected(bundle connectionhint) { // save credentials. person currentperson = plus.peopleapi.getcurrentperson(mgoogleapiclient); sharedpreferenceshelper.updatestringvalue( loginasverifiedtracker.this, r.string.preferences_user_id, currentperson.getid()); sharedpreferenceshelper.updatestringvalue( loginasverifiedtracker.this, r.string.preferences_user_name, currentperson.getdisplayname()); // close. setresult(activity.result_ok); finish(); return; } @override public void onconnectionsuspended(int cause) { mgoogleapiclient.connect(); }

edit:

here onactivityresult class:

@override protected void onactivityresult(int requestcode, int resultcode, intent data) { switch (requestcode) { case rc_sign_in: { mintentinprogress = false; if (!mgoogleapiclient.isconnecting()) { mgoogleapiclient.connect(); } break; } case 1: { overridependingtransition(r.anim.slide_right_to_left_enter, r.anim.slide_right_to_left_exit); break; } } }

this dialog i'm talking about:

per step 5 of google+ sign in guide:

you should reset state of flags when command returns activity in onactivityresult.

protected void onactivityresult(int requestcode, int responsecode, intent intent) { if (requestcode == rc_sign_in) { if (responsecode != result_ok) { msigninclicked = false; } mintentinprogress = false; if (!mgoogleapiclient.isconnecting()) { mgoogleapiclient.connect(); } } }

note check against responsecode - when user has nail sign in button responsecode equal result_ok. ensures cancel button stops resolvesigninerror() phone call in onconnectionfailed() (which causes loop forever).

android google-plus google-play-services

No comments:

Post a Comment