java - If shared preferences does not contain -


    sharedpreferences mprefs = getsharedpreferences("idvalue",0);      if(mprefs.contains("date")) {     //do     }else {         mprefs.edit().putstring("date", currentdate);         mprefs.edit().commit();         toast.maketext(this, "changed", toast.length_short).show();     } 

what want code run first time , show toast second time run not show , run code inside first parameters. code runs second "else" statement twice , doesn't run first. it's if string isn't being put in "date"? there wrong code?

make editor object , use edit/commit.

you can use below code. work fine.

sharedpreferences mprefs = getsharedpreferences("idvalue",0); sharedpreferences.editor meditor = mprefs.edit();  if(mprefs.contains("date")) {             //do }else {      meditor.putstring("date", currentdate);      meditor.commit();      toast.maketext(this, "changed", toast.length_short).show(); } 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

android - CoordinatorLayout, FAB and container layout conflict -