java - Entire SharedPreference not getting cleared -


i creating 2 sharedpreferences file maintain sessions in app.

while create logout functionality in app, sharedpreferences not cleared , when next user tries login, still sees previous user details or no detials @ all.

here 2 files sharedpreferences:-

storagehelper.java

public class storagehelper  {     private static context mycontext = login.getmycontext();      public static login mylogin = new login();      public string deviceid;      public static string userdatafile = "userdata";      sharedpreferences localsettings;      sharedpreferences.editor editor;      public storagehelper(string deviceid)      {         // todo auto-generated constructor stub         deviceid = deviceid;     }       public storagehelper()      {         // todo auto-generated constructor stub     }       public  void setdeviceid(string deviceid)     {         // todo auto-generated method stub           localsettings = mycontext.getsharedpreferences(userdatafile,0);          editor = localsettings.edit();         editor.putstring("deviceid",  deviceid);         editor.commit();      }        public  string getdeviceid()       {           string deviceid = "";           localsettings = mycontext.getsharedpreferences(userdatafile,0);           if (localsettings.contains("deviceid"))           {               deviceid = localsettings.getstring("deviceid", "nothing found");           }           return deviceid;       }      public static boolean getregistrationinformation()      {         // todo auto-generated method stub         return true;     }      public  void setregistrationinformation(boolean value)      {         // todo auto-generated method stub          localsettings = mycontext.getsharedpreferences(userdatafile,0);          editor = localsettings.edit();        editor.putboolean("registration", value);        editor.commit();       }      public void cleardata()      {          localsettings = mycontext.getsharedpreferences(userdatafile,0);          editor = localsettings.edit();         editor.clear();         editor.commit();     }  } 

sessionmanagement.java

 sharedpreferences pref;          // editor shared preferences         editor editor;          // context         context _context;          // shared pref mode         int private_mode = 0;          // sharedpref file name         private static final string pref_name = "userdetails";          // shared preferences keys         private static final string is_login = "isloggedin";          // user name (make variable public access outside)         public static final string key_emailid = "email";          // email address (make variable public access outside)          public static final string key_devicename = "devicename";         public static final string key_usersname = "usersname";           public static final string  key_deviceregistered = "deviceregistered";         // constructor         public sessionmanagement(context context)         {             this._context = context;             pref = _context.getsharedpreferences(pref_name, private_mode);             editor = pref.edit();         }          public void createloginsession(string emailid, string deviceauthurl, string deviceid, string endpointhost, string devicename, string usersname, string encodedaccountname, string hosturl)         {             // storing login value true             editor.putboolean(is_login, true);                         editor.putstring(key_devicename,devicename);             editor.putstring(key_usersname, usersname);               // commit changes             editor.commit();         }          /**          * stored session data          * */         public hashmap<string, string> getuserdetails()         {             hashmap<string, string> user = new hashmap<string, string>();              user.put(key_devicename, pref.getstring(key_devicename, null));             user.put(key_usersname, pref.getstring(key_usersname, null));               // return user             return user;         }         /**          * check login method wil check user login status          * if false redirect user login page          * else won't          * */         public void checklogin()         {             // check login status             if(!this.isloggedin())             {                 // user not logged in redirect him login activity                 intent = new intent(_context, login.class);                 // closing activities                 i.addflags(intent.flag_activity_clear_top);                  // add new flag start new activity                 i.setflags(intent.flag_activity_new_task);                  // staring login activity                   _context.startactivity(i);             }          }         // function clears session data , redirect user loginactivity         /**              * clear session details              * */             public void logoutuser()             {                 // clearing data shared preferences                 //editor.clear();                  editor.remove(key_devicename);                  editor.remove(key_usersname);                 e                 editor.remove(pref_name);                 editor.commit();                  // after logout redirect user loing activity                 intent = new intent(_context, login.class);                 // closing activities                 i.addflags(intent.flag_activity_clear_top);                  // add new flag start new activity                 i.setflags(intent.flag_activity_new_task);                  // staring login activity                 _context.startactivity(i);             }              public boolean isloggedin()             {                 return pref.getboolean(is_login, false);             } 

i cleared data like:-

  case r.id.action_settings:          storagehelper helper  = new storagehelper();         helper.cleardata();          sessionmanagement session = new sessionmanagement(getapplicationcontext());         session.logoutuser();          finish();          return true; 

from docs:

"unlike commit(), writes preferences out persistent storage synchronously, apply() commits changes in-memory sharedpreferences "

also docs:

"for particular set of preferences, there single instance of class (sharedpreferences) clients share"

this means trying modify same (and only) copy of sharedpreferences 2 different locations , since commit() writes disk (which expensive) there inconsistency (last commit take precedence).

first step solve issue realize have 1 instance of sharedpreferences , you're accessing same instance different places - might create race-condition. considering that, right thing re-design software consider it!

you can try , use apply() before use commit supposed safer (since changes "in-memory" object faster write-to-disk), or add synchronization mechanism - incorrect use of sharedpreferences, if patches problem.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -