android - second activity still remains in session after logout -
i developing apps in android. in have login , logout activity session management using sharedpreferences. apps logout , store data in sharedpreferences maintain session done. when logout apps(second activity) , rendered first activity(login activity ) happen when clicked button on emulator after logout still again rendered second activity , version 2.3.3 etc encountered exception
java.lang.runtimeexception: unable destroy activity {com.oj.bs/com.oj.bs.loginactivity}: java.lang.nullpointerexception
can fix problem. please refer following code
following login activity code
sessionmngr = new sessionmanager(getapplicationcontext()); jsonobject json_user = jsonobj.getjsonobject("user"); sessionmngr.createloginsession(id,json_user.getstring(key_uname), json_user.getstring(key_uemail)); . .
following session management
public class sessionmanager { sharedpreferences pref; editor editor; context contxt; //shared pref mode int private_mode = 0; // sharedpref file name private static final string pref_name = "bookingscapepref"; // shared preferences keys private static final string is_login = "isloggedin"; public static final string key_name = "name"; public static final string key_id = "id"; public static final string key_email = "email"; public sessionmanager(context context) { super(); this.contxt = context; pref = context.getsharedpreferences(pref_name, private_mode); editor = pref.edit(); editor.commit(); } /** * create login session * */ public void createloginsession(int id,string name, string email) { editor.putboolean(is_login,true); editor.putstring(key_name, name); editor.putstring(key_email, email); editor.putint(key_id, id); editor.commit(); } /** * stored session data * */ public hashmap<string, string> getuserdetails() { hashmap<string, string> userdetails = new hashmap<string, string>(); userdetails.put(key_name, pref.getstring(key_name, null)); userdetails.put(key_email, pref.getstring(key_email, null)); return userdetails; } /** * check login method check user login status * */ public void checklogin() { if (!this.isloggedin()) { intent = new intent(contxt, loginactivity.class); //closing activity i.addflags(intent.flag_activity_clear_top); //add new flag start new activity i.setflags(intent.flag_activity_new_task); contxt.startactivity(i); } } public void logoutuser() { //clearing data sharedpreferences editor.clear(); editor.commit(); intent intnt = new intent(contxt, loginactivity.class); // closing activities intnt.addflags(intent.flag_activity_clear_top); // add new flag start new activity intnt.setflags(intent.flag_activity_new_task); // staring login activity contxt.startactivity(intnt); } /** * quick check login * **/ // login state public boolean isloggedin(){ return pref.getboolean(is_login, false); }
}
this second activity here i'm checking session
public class projectfragactivity extends sherlockfragmentactivity { sessionmanager sessionmngr ; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); sessionmngr = new sessionmanager(getapplicationcontext()); toast.maketext(getapplicationcontext(), "user login status: " + sessionmngr .isloggedin(), toast.length_long).show(); sessionmngr.checklogin(); . .
i don't know how session , working, bt, try use finish() function while doing logout action, action not there while button presses
Comments
Post a Comment