Google+ Login Not working properly on Android fragment -
i working google+ login application , when done using activity work charm , after move code fragment , after when try login google+ not working have open fragment activity 2 times login google+ can tell me happen code fragment added below
public class googleplusefragment extends fragment implements connectioncallbacks, onconnectionfailedlistener { private static final int rc_sign_in = 0; private static final string tag = "mainactivity"; private static final int profile_pic_size = 800; private googleapiclient mgoogleapiclient; private boolean mintentinprogress; private boolean msigninclicked; private connectionresult mconnectionresult; private signinbutton btnsignin; private button btnsignout; private context mcontext; private activity mactivity; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); mactivity = getactivity(); mcontext = getactivity().getapplicationcontext(); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.compund_google_pluse_fragment, container, false); btnsignin = (signinbutton) view.findviewbyid(r.id.btn_sign_in); btnsignout = (button) view.findviewbyid(r.id.btn_sign_out); sharedpref = view.getcontext().getsharedpreferences( constantz.sheared_prefereance, context.mode_private); mgoogleapiclient = new googleapiclient.builder(view.getcontext()) .addconnectioncallbacks(this) .addonconnectionfailedlistener(this).addapi(plus.api, null) .addscope(plus.scope_plus_login).build(); btnsignin.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { signinwithgplus(); } }); btnsignout.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { signoutfromgplus(); } }); return view; } @override public void onstart() { super.onstart(); mgoogleapiclient.connect(); } @override public void onstop() { super.onstop(); if (mgoogleapiclient.isconnected()) { mgoogleapiclient.disconnect(); } } @override public void onactivityresult(int requestcode, int responsecode, intent intent) { if (requestcode == rc_sign_in) { if (responsecode != activity.result_ok) { msigninclicked = false; } mintentinprogress = false; if (!mgoogleapiclient.isconnecting()) { mgoogleapiclient.connect(); } } } @override public void onconnectionfailed(connectionresult result) { if (!result.hasresolution()) { googleplayservicesutil.geterrordialog(result.geterrorcode(), mactivity, 0).show(); log.e(tag, "" + result.geterrorcode()); return; } if (!mintentinprogress) { mconnectionresult = result; if (msigninclicked) { log.e(tag, "" + result.geterrorcode()); resolvesigninerror(); } } } @override public void onconnected(bundle arg0) { msigninclicked = false; getprofileinformation(); updateui(true); } @override public void onconnectionsuspended(int arg0) { mgoogleapiclient.connect(); updateui(false); } private void updateui(boolean issignedin) { if (issignedin) { btnsignin.setvisibility(view.gone); btnsignout.setvisibility(view.visible); } else { btnsignin.setvisibility(view.visible); btnsignout.setvisibility(view.gone); } } /** * sign-in google * */ private void signinwithgplus() { if (!mgoogleapiclient.isconnecting()) { msigninclicked = true; resolvesigninerror(); } } /** * method resolve signin errors * */ private void resolvesigninerror() { if (mconnectionresult.hasresolution()) { try { mintentinprogress = true; mconnectionresult.startresolutionforresult(mactivity, rc_sign_in); } catch (sendintentexception e) { mintentinprogress = false; mgoogleapiclient.connect(); } } } /** * fetching user's information name, email, profile pic * */ private void getprofileinformation() { try { if (plus.peopleapi.getcurrentperson(mgoogleapiclient) != null) { person currentperson = plus.peopleapi .getcurrentperson(mgoogleapiclient); string personname = currentperson.getdisplayname(); string personphotourl = currentperson.getimage().geturl(); string persongoogleplusprofile = currentperson.geturl(); string email = plus.accountapi.getaccountname(mgoogleapiclient); log.e(tag, "name: " + personname + ", plusprofile: " + persongoogleplusprofile + ", email: " + email + ", image: " + personphotourl + " user id:" + currentperson.getid()); } else { toast.maketext(mcontext, "person information null", toast.length_long).show(); } } catch (exception e) { e.printstacktrace(); } } /** * sign-out google * */ private void signoutfromgplus() { if (mgoogleapiclient.isconnected()) { plus.accountapi.cleardefaultaccount(mgoogleapiclient); mgoogleapiclient.disconnect(); mgoogleapiclient.connect(); updateui(false); } }
}
this how added framgent in fragment activity
plusefragment = new googleplusefragment(); fragmentmanager manager = getsupportfragmentmanager(); fragmenttransaction transaction = manager.begintransaction(); transaction.add(r.id.pluse_frame_layout, plusefragment); transaction.commit();
can tell me have done wrong ? why have open activity 2 times login thank
finally found answer, problem when result activity call in fragment catch parent activity have manually redirect result fragment. have add line in parent fragment activity
@override protected void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == googleplusefragment.rc_sign_in) { googleplusefragment fragment = (googleplusefragment) getsupportfragmentmanager() .findfragmentbyid(r.id.pluse_frame_layout); fragment.onactivityresult(requestcode, resultcode, data); } else { super.onactivityresult(requestcode, resultcode, data); } }
Comments
Post a Comment