android - SharedPreferences from one Fragment isn't registering data for a Login Activity on another Fragment -


i have fragment called loginfragment through user can register first time clicking on register button. upon clicking button new fragment, called registerfragment, opens user chooses , enters new username , password registered. when run app , enter fields under register , select ok, shows "saved info" in toast it's supposed to. when try logging in using username , password registered for, app doesn't recognize info , displays toast "please check entries , try again" should displayed when username exists or password incorrect, in case not case.

is sharedpreferences not saving username , password values because working 2 different fragments? appreciated!

registerfragment.java

   public class registerfragment extends fragment {      button register_btn;     edittext username_signup_et, password_signup_et;      private  string key_uid = "uid";     private  string key_password = "pwd";     private  final string prefer_name = "registry";     private sharedpreferences sharedpreferences;     private sharedpreferences.editor editor;     string username;      //logindatabaseadapter logindatabaseadapter;     context context;     public void myalertdialog(){     context = getactivity();     }     public view oncreateview(layoutinflater inflater, viewgroup container,             bundle savedinstancestate){          view v = inflater.inflate(r.layout.register_fragment_layout, container, false);          sharedpreferences = getactivity().getsharedpreferences("loginprefs", context.mode_private);;         editor = sharedpreferences.edit();           username_signup_et = (edittext)v.findviewbyid(r.id.username_signup_et);         password_signup_et = (edittext)v.findviewbyid(r.id.password_signup_et);          register_btn = (button)v.findviewbyid(r.id.register_btn);         register_btn.setonclicklistener(new onclicklistener(){              @override             public void onclick(view v) {              string username = username_signup_et.gettext().tostring();             string password = password_signup_et.gettext().tostring();              if(username.equals("")||password.equals(""))             {                 toast.maketext(getactivity(), "please enter values in fields!", toast.length_short).show();             }             else{             register();             launchlogin();             }                }         });         return v;        }      //launch login activity     public void launchlogin(){         fragmentmanager fm = getfragmentmanager();         fragmenttransaction ft = fm.begintransaction();         loginfragment lg = new loginfragment();         ft.addtobackstack("lg");         ft.replace(r.id.fragment_one_id, lg);         ft.commit();     }      // register account             private void register() {                 // prefix keys user name                 editor.putstring("username" + "_" + key_uid, username_signup_et.gettext()                         .tostring());                 editor.putstring("password" + "_" + key_password, password_signup_et.gettext()                         .tostring());                 //editor.commit(); // commit values                 editor.apply();                  toast.maketext(getactivity(), "saved info!", toast.length_short).show();              }// end of register  } 

loginfragment.java

    public class loginfragment extends fragment {       button login_btn, register_btn;     edittext username_et, password_et;      private  string key_uid = "uid";     private  string key_password = "pwd";     private  final string prefer_name = "registry";     private sharedpreferences sharedpreferences;     private sharedpreferences.editor editor;      string username;      public view oncreateview(layoutinflater inflater, viewgroup container,             bundle savedinstancestate){         super.oncreateview(inflater, container, savedinstancestate);          view v = inflater.inflate(r.layout.login_fragment_layout, container, false);          sharedpreferences = getactivity().getsharedpreferences("loginprefs", context.mode_private);         editor = sharedpreferences.edit();          username_et = (edittext)v.findviewbyid(r.id.username_et);         password_et = (edittext)v.findviewbyid(r.id.password_et);         login_btn = (button)v.findviewbyid(r.id.login_btn);         register_btn = (button)v.findviewbyid(r.id.register_btn);           login_btn.setonclicklistener(new onclicklistener(){              @override             public void onclick(view v) {                 // initialize username                 username = username_et.gettext().tostring();                  boolean valid = isvalid();                 if (valid) {                     string text = getresources().getstring(r.string.logging_in_str);                     toast.maketext(getactivity().getapplicationcontext(), text,                             toast.length_short).show();                     if (!userexists() | !(passwordcorrect())) {                         text = getresources().getstring(                                 r.string.error_username_str);                         toast.maketext(getactivity().getapplicationcontext(), text,                                 toast.length_short).show();                     } else {                         intent = new intent(getactivity(), diaryactivity.class);                         startactivity(i);                     }                 }                   //sharedpreferences = getactivity().getsharedpreferences("loginprefs", context.mode_private);                  //get username , password edittext                 string username = username_et.gettext().tostring();                 string password = password_et.gettext().tostring();                   if(username_et.length() > 0 && password_et.length() > 0) {                     string uname = null;                     string upassword = null;                        if (sharedpreferences.contains(username)) {                         uname = sharedpreferences.getstring("username", "");                      }                      if (sharedpreferences.contains(password)) {                         upassword = sharedpreferences.getstring("password", "");                      }                      //start new activity                     if (username.equals(uname) && password.equals(upassword)) {                           toast.maketext(getactivity(), "you entered: " + uname, toast.length_short).show();                         intent = new intent(getactivity(), diaryactivity.class);                         startactivity(i);                     }                 }                 //string username = sharedpreferences.getstring("username", "");                 //string pw = sharedpreferences.getstring("password", "");                  //sharedpreferences.getstring("uid", "");                 //sharedpreferences.getstring("pwd", "");             }            });          register_btn.setonclicklistener(new onclicklistener(){              @override             public void onclick(view v) {                 fragmentmanager fm = getfragmentmanager();                 fragmenttransaction ft = fm.begintransaction();                 registerfragment rg = new registerfragment();                 ft.replace(r.id.register_fragment_id, rg);                 ft.commit();             }            });         return v;     }      // getter user name     public  string getusername() {         string str = username_et.gettext().tostring();         return str;// returns user name     }// end of getusername()      private boolean passwordcorrect() {          // prefix key user name         if (sharedpreferences.contains(username + "_" + key_uid)) {             string pwd = sharedpreferences.getstring(username + "_"                     + key_password, "");             if (pwd.equals(password_et.gettext().tostring()))                 return true;         }             return false;      }     // getter user name      private boolean userexists() {          // prefix key user name         if (sharedpreferences.contains(username + "_" + key_uid)) {             string uid = sharedpreferences.getstring(username + "_" + key_uid,                     "");             if (uid.equals(username_et.gettext().tostring()))                 return true;         }              return false;      }// end of userexists()      private boolean isvalid() {         boolean idempty = false;         boolean passempty = false;         if ((username_et.gettext().tostring().isempty())) {             idempty = true;             string text = getresources()                     .getstring(r.string.error_emptyusername);             toast.maketext(getactivity().getapplicationcontext(), text, toast.length_short)                     .show();         }         if ((password_et.gettext().tostring().isempty())) {             passempty = true;             string text = getresources()                     .getstring(r.string.error_emptypassword);             toast.maketext(getactivity().getapplicationcontext(), text, toast.length_short)                     .show();         }         return !(idempty | passempty);     }// end of isvalid()  } 

you using different key data in loginfragment. ie trying access preference not exist. change code below

loginfragment:

private boolean userexists() {      string uid = sharedpreferences.getstring("username" + "_" + key_uid, "");      return (uid.equals(username_et.gettext().tostring()))  } 

change password well.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -