sharedpreferences - Google Sign-in "check first time login" and "Logout" issues in android application -


i making android application, when user installs application, he/she needs sign-in google account, after successful logged-in , he/she redirected dashboard page. if he/she closes application , opens again , if logged in dashboard activity should shown itself. have drawerlayout in dashboard activity having "sign-out" button user can log out , login activity opened again.

  • i using sharedpreference checking first time login in android

    below problems facing

  • application opens dashboard activity without asking login.

  • when "sign-out" clicked drawer layout in dashboard activity, second goes login activity automatically log-in again. in short "sign-out" not working, keeps me log-in.

below screenshots when user opens app first time. doesn't ask me login. it doesn't ask log-in

but when clicks on signout, login activity shown, enter image description here

below have shown screenshot or result, everytime try logout log-in me again. inputs appreciated. enter image description here

loginactivity.java

public class googleplayservicesactivity extends activity implements onclicklistener, connectioncallbacks, onconnectionfailedlistener {      private static final int rc_sign_in = 0;     private static final string tag = "googlelogin";      // google client communicate google     public googleapiclient mgoogleapiclient;     private boolean mintentinprogress;     private boolean signedinuser;     private connectionresult mconnectionresult;     private signinbutton signinbutton;     private textview username, emaillabel;     private linearlayout signinframe;     private relativelayout profileframe;     private view relativelayout;     private string personphotourl;     private string personid;     private string personname;     private string personemail;     private boolean hasloggedin;     private string persongender;     private string persondob;     private string personfullname;     private string user_gender = "";     // databasehandler dbhandler = new databasehandler(this);    public void checklogin(){        log.d(tag,"checked logged in called ");        sharedpreferences settings = getsharedpreferences(constants.prefs_name, mode_private);        hasloggedin = settings.getboolean("hasloggedin", false);        log.d("value of hasloggedin",":"+string.valueof(hasloggedin));        if(hasloggedin){            intent = new intent(getapplicationcontext(),activityfeedactivity.class);            startactivityfeedactivity(i);            googleplayservicesactivity.this.finish();        }    }      private void startactivityfeedactivity(intent i) {         startactivity(i);     }      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         //checklogin();         setcontentview(r.layout.google_login);         //initfacebooklogin();         signinbutton = (signinbutton) findviewbyid(r.id.signin);         signinbutton.setonclicklistener(this);         relativelayout = getlayoutinflater().inflate(r.layout.profile_view, null);         username = (textview) relativelayout.findviewbyid(r.id.name);         emaillabel = (textview)  relativelayout.findviewbyid(r.id.email);          profileframe = (relativelayout) relativelayout.findviewbyid(r.id.profileview);         signinframe = (linearlayout) findviewbyid(r.id.signinframe);          mgoogleapiclient = new googleapiclient.builder(this).addconnectioncallbacks(this).addonconnectionfailedlistener(this).addapi(plus.api, plus.plusoptions.builder().build()).addscope(plus.scope_plus_login).build();     }      protected void onstart() {         super.onstart();         mgoogleapiclient.connect();     }      protected void onstop() {         super.onstop();         if (mgoogleapiclient.isconnected()) {             mgoogleapiclient.disconnect();         }     }      private void resolvesigninerror() {         if (mconnectionresult.hasresolution()) {             try {                 mintentinprogress = true;                 mconnectionresult.startresolutionforresult(this, rc_sign_in);             } catch (sendintentexception e) {                 mintentinprogress = false;                 mgoogleapiclient.connect();             }         }     }      @override     public void onconnectionfailed(connectionresult result) {         if (!result.hasresolution()) {             googleplayservicesutil.geterrordialog(result.geterrorcode(), this, 0).show();             return;         }          if (!mintentinprogress) {             // store mconnectionresult             mconnectionresult = result;              if (signedinuser) {                 resolvesigninerror();             }         }     }      @override     protected void onactivityresult(int requestcode, int responsecode, intent intent) {         switch (requestcode) {             case rc_sign_in:                 if (responsecode == result_ok) {                     signedinuser = false;                  }                 mintentinprogress = false;                 if (!mgoogleapiclient.isconnecting()) {                     mgoogleapiclient.connect();                 }                 break;         }     }      @override     public void onconnected(bundle arg0) {         signedinuser = false;         getprofileinformation();         sharedpreferences settings = getsharedpreferences(constants.prefs_name, mode_private); // 0 - private mode         sharedpreferences.editor editor = settings.edit();         editor.putboolean("hasloggedin", true);         editor.putstring("personid", getpersonid());         editor.putstring("personname", getpersonfullname());         editor.putstring("personemail",getpersonemail());         editor.putstring("picurl",getpicurl());         editor.commit();         intent = new intent(this,activityfeedactivity.class);         startactivityfeedactivity(i);         googleplayservicesactivity.this.finish();     }       private void getprofileinformation() {         try {             if (plus.peopleapi.getcurrentperson(mgoogleapiclient) != null) {                 person currentperson = plus.peopleapi.getcurrentperson(mgoogleapiclient);                  string personid = currentperson.getid();                 string personfullname = currentperson.getdisplayname(); // gives fullname                 string personname = string.valueof(currentperson.getname()); //gives first name , last name in json formay                 string persongender = string.valueof(currentperson.getgender()); //gives gender in int 0:male, 1:female                 string persondob = currentperson.getbirthday();                 string email = plus.accountapi.getaccountname(mgoogleapiclient);                 string personpicurl = currentperson.getimage().geturl();                  setpersonid(currentperson.getid());                 setpersonfullname(currentperson.getdisplayname());                 setpersonname(currentperson.getname());                 setpersonemail(email);                 setpersongender(persongender);                 setpicurl(currentperson.getimage().geturl());                 setpersondob(currentperson.getbirthday());                  log.d(tag, "gender:" + persongender + " dob:" + persondob + " firstname:" + personname + "  ");                 username.settext(personfullname);                 emaillabel.settext(email);             }         } catch (exception e) {             e.printstacktrace();         }     }      @override     public void onconnectionsuspended(int cause) {         mgoogleapiclient.connect();        // updateprofile(false);     }      @override     public void onclick(view v) {         switch (v.getid()) {             case r.id.signin:                 googlepluslogin();                 break;         }     }      private void googlepluslogin() {         if (!mgoogleapiclient.isconnecting()) {             signedinuser = true;             resolvesigninerror();         }     }  } 

dashboardactivity.java

private void initdrawerlist(string[] values){         this.drawerlist = (listview) findviewbyid(r.id.navdrawer);         final drawerlayout layout = this.drawerlayout;         arrayadapter<string> adapter = new arrayadapter<string>(this,                 android.r.layout.simple_list_item_1, android.r.id.text1, values);         this.drawerlist.setadapter(adapter);         this.drawerlist.setonitemclicklistener(new adapterview.onitemclicklistener() {             @override             public void onitemclick(adapterview<?> parent, view view,                                     int position, long id) {                 switch (position) {                     case 0:                         layout.closedrawer(gravity.start);                         break;                     case 1:                         layout.closedrawer(gravity.start);                         break;                     case 2:                         layout.closedrawer(gravity.start);                         break;                     case 3:                         googlelogout();                         log.d(tag,"returned function");                         break;                 }             }         });     }      private void googlelogout() {         sharedpreferences settings = getsharedpreferences(constants.prefs_name, mode_private);         sharedpreferences.editor editor = settings.edit();         editor.clear();         editor.commit();         if (mgoogleapiclient.isconnected()){             plus.accountapi.cleardefaultaccount(mgoogleapiclient);             mgoogleapiclient.disconnect();             mgoogleapiclient.connect();             intent = new intent(this,googleplayservicesactivity.class);             startactivity(i);             activityfeedactivity.this.finish();         }         else {             log.d(tag,"entered else");         }          //updateprofile(false);     } 

try method while click on logout

sign-out google

private void signoutfromgplus(){         if (mgoogleapiclient.isconnected(){             plus.accountapi.cleardefaultaccount(mgoogleapiclient);             mgoogleapiclient.disconnect();             mgoogleapiclient.connect();         } } 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -