android - how to do a login greeting (toast) and don't have to type again when user log in -


i make userlogin details, using shared preferences.

when load application login page appear. after i've typed in , toast greeting user, such welcome [username]. when user login again, toast message welcome [username]. user not have type his/her name again go in application. in fact, when user click login, toast welcome [username], , he/she can continue application.

however, in code, skip login page , , didn't toast name. did log.d nothing appear in logcat. can please me out?

i think there's not right retrieve preference, not sure how can edit it, i'm new android.

here code:

protected void oncreate(bundle savedinstancestate)  {      super.oncreate(savedinstancestate);      setcontentview(r.layout.login);       username =(edittext)findviewbyid(r.id.nameedittext);      loginbutton = (button)findviewbyid(r.id.loginbtn);      loginbutton.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                  intent intent=new intent(getapplicationcontext(),homeactivity.class);                 startactivity(intent);           bundle extras = new bundle();           extras.putstring("name",username.gettext().tostring());           intent.putextras(extras);           startactivityforresult(intent, request_code);      }             });  }  @override protected void ondestroy() {     // todo auto-generated method stub     super.ondestroy(); }  @override protected void onpause() {     // todo auto-generated method stub     super.onpause();     saveaspreferences(); }  @override protected void onrestart() {     // todo auto-generated method stub     super.onrestart(); }  @override protected void onresume() {     // todo auto-generated method stub     super.onresume();     retrievepreferences(); }  @override protected void onstart() {     // todo auto-generated method stub     super.onstart(); }  @override protected void onstop() {     // todo auto-generated method stub     super.onstop(); }  public void saveaspreferences() {     string name = username.gettext().tostring();      sharedpreferences prefs = getsharedpreferences("preferences",             mode_private);      sharedpreferences.editor editor = prefs.edit();      editor.putstring("name", name);      editor.commit();  }// saveaspreferences  public void retrievepreferences() {     sharedpreferences prefs = getsharedpreferences("preferences",             mode_private);      if (prefs.contains("name")) {         string name = prefs.getstring("name", "");         log.d("login","name : "+name);         username.settext(name);         toast.maketext(getapplicationcontext(), "welcome" + name, toast.length_short).show();           intent intent=new intent(getapplicationcontext(),homeactivity.class);           startactivity(intent);             }           else           {                  if (prefs.contains("name")) {                     string name = prefs.getstring("name", "");                     log.d("else","name : "+name);                     username.settext(name);           }      }      } } 

hom.java

public class homeactivity extends activity{     button btnlogin;     button btnabout;     textview name;     final context context = this;      @override     protected void oncreate(bundle savedinstancestate)      {          super.oncreate(savedinstancestate);          setcontentview(r.layout.homescreen);          name = (textview) findviewbyid(r.id.name);                  bundle extras = getintent().getextras();          if(extras != null)          {              string username = extras.getstring("name");              name.settext(username);          } 

i looked code.why start home activity twice in onclicklistener ?

 intent intent=new intent(getapplicationcontext(),homeactivity.class);  startactivity(intent);  bundle extras = new bundle();  extras.putstring("name",username.gettext().tostring());  intent.putextras(extras);  startactivityforresult(intent, request_code); 

you had started startactivity(intent); , startactivityforresult(intent, request_code);.i guess causes problem.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -