Android: Update SharedPreferences -


i'm trying keep counter of times application lauched. that, need keep information stored in device, once application has been killed.

i read link:

http://developer.android.com/guide/topics/data/data-storage.html#pref

and thought easiest way implement this, using sharedpreferences. wrote code:

final string prefs_name = "myprefsfile";   sharedpreferences settings = getapplicationcontext().getsharedpreferences(prefs_name, 0); int timeslaunched = settings.getint("timeslaunched", 0);  timeslaunched++;  sharedpreferences.editor editor = settings.edit(); editor.putint("timeslaunched", timeslaunched);   editor.commit(); 

i found out works first time, chages 0 1, all. following times launched app nothing happened.

does know wrong?

here posting class wrote save user credentials when loggedin app modify way want

public class sessionmanager { // shared preferences 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 = "tgcprefs";  // shared preferences keys private static final string is_login = "isloggedin";  // user name (make variable public access outside) public static final string key_username = "email";  // email address (make variable public access outside) public static final string key_password = "password";  // constructor public sessionmanager(context context) {     this._context = context;     pref = _context.getsharedpreferences(pref_name, private_mode);     editor = pref.edit(); }  /**  * create login session  * */ public void createloginsession(string email, string password , boolean facebookcall) {     // storing login value true     editor.putboolean(is_login, true);      // storing name in pref     editor.putstring(key_username, email);      // storing email in pref     editor.putstring(key_password, password);      editor.putboolean("fb", facebookcall);     // commit changes     editor.commit(); }  /**  * check login method wil check user login status if false redirect  * user login page else won't  * */ public boolean checklogin() {     // check login status     if (!this.isloggedin()) {     /*  // user not logged in redirect him login activity         intent = new intent(_context, loginactivity.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);*/         return false;     } else if (this.isloggedin()) {         return true;     }     return false; }   public boolean isfacebookloggedid(){     return pref.getboolean("fb", false); }  /**  * stored session data  * */ public hashmap<string, string> getuserdetails() {     hashmap<string, string> user = new hashmap<string, string>();     // user name     user.put(key_username, pref.getstring(key_username, null));      // user email id     user.put(key_password, pref.getstring(key_password, null));        // return user     return user; }  /**  * clear session details  * */ public void logoutuser() {     // clearing data shared preferences     editor.clear();     editor.commit();      // after logout redirect user loing activity     /*intent = new intent(_context, loginactivity.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);*/ }  /**  * quick check login  * **/ // login state public boolean isloggedin() {     return pref.getboolean(is_login, false); } } 

all need modify , call prefs last counter , increment 1 on splash screen if how working or strategy want use


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 -