How to keep information about an app in Android? -
i want know if possible keep information app in while example.
i have app access file , information choices user have made. example:
i have button many events (event model), , want know if user clicked in button after application restarts.
i know possible keep information login , password. possible other information?
use shared preferences. so:
create these methods use, or use content inside of methods whenever want:
public string getprefvalue() { sharedpreferences sp = getsharedpreferences("preferencename", 0); string str = sp.getstring("mystore","thedefaultvalueifnovaluefoundofthiskey"); return str; } public void writetopref(string thepreference) { sharedpreferences.editor pref =getsharedpreferences("preferencename",0).edit(); pref.putstring("mystore", thepreference); pref.commit(); }
you call them this:
// when click button: writetopref("theyclickedthebutton"); if (getprefvalue().equals("theyclickedthebutton")) { // have clicked button } else if (getprefvalue().equals("thedefaultvalueifnovaluefoundofthiskey")) { // preference has not been created (have not clicked button) } else { // preference has been created, have not clicked button }
explanation of code:
"preferencename"
name of preference you're referring , therefore has same every time access specific preference. eg: "password"
, "theirsettings"
"mystore" refers specific string
stored in preference , can multiple. eg: have preference "theirsettings"
, "mystore"
"soundprefs"
, "colourprefs"
, "language"
, etc.
note: can boolean
, integer
, etc.
all have change string
storing , reading boolean
, or whatever type want.
Comments
Post a Comment