Android Java sharedPreferences logic issue -
android java sharedpreferences
logic issue
i have app has sign in , sign out in it.
i want, when user first downloads app, sign out. when signs in using button, sharedpreference
value saves. next time opens app, automatically sign him in. lets signs out closes app, when opens it, sign him out. thats how goes apps.
what did, @ first of activity added this
sharedpreferences siggnedin = getsharedpreferences("yesorno", activity.mode_private); sharedpreferences.editor editor = siggnedin.edit(); editor.putboolean("siggnedin?", false); editor.commit();
then when user clicks button signs out
sharedpreferences siggnedin = getsharedpreferences("yesorno", activity.mode_private); sharedpreferences.editor editor = siggnedin.edit(); editor.putboolean("siggnedin?", false); editor.commit();
and if clicks sign in
sharedpreferences siggnedin = getsharedpreferences("yesorno", activity.mode_private); sharedpreferences.editor editor = siggnedin.edit(); editor.putboolean("siggnedin?", true); editor.commit();
and @ last, if have method updates every second.
so in it, add
sharedpreferences siggnedin = getsharedpreferences("yesorno", activity.mode_private); boolean myintvalue = siggnedin.getboolean("siggnedin?", false); if(myintvalue){ signhimin(); }
that doesn't work.
keep things simple
check status
sharedpreferences pref = getsharedpreferences("usersession", activity.mode_private); boolean isloggedin = pref.getboolean("isloggedin", false);
sign in
sharedpreferences pref = getsharedpreferences("usersession", activity.mode_private); sharedpreferences.editor editor = pref.edit(); editor.putboolean("isloggedin", true); editor.commit();
sign out
sharedpreferences pref = getsharedpreferences("usersession", activity.mode_private); sharedpreferences.editor editor = pref.edit(); editor.putboolean("isloggedin", false); editor.commit();
simple logic
protected boolean isusersignedin(){ boolean isloggedin = false; sharedpreferences pref = getsharedpreferences("usersession", activity.mode_private); isloggedin = pref.getboolean("isloggedin", false);//false default return isloggedin; }
usage
if(!isusersignedin()){ signhimin(); }
Comments
Post a Comment