java - How to save textfield response without clearing -


i trying create simple list style app. have saved string using sharedpreferences, able refer in logs, make string stays attached textfield instead of being cleared every time app reopened (until changed or deleted user). other suggestions appreciated.

(mainactivity)

public void go(view view) {      intent intent1 = new intent(secondactivity.this, mypreferences.class);      string passgoal1 = goal1.gettext().tostring();      intent1.putextra("goal1", passgoal1);      startactivity(intent1); } 

also implemented mypreferences class referenced in first answer.

public class mypreferences {      private sharedpreferences pref;      private sharedpreferences.editor editor;      private context context;      int private_mode = 0;      private static final string pref_name = "mypreferencesname";      public final static string key_name = "key_value";      public mypreferences(context context) {          this.context = context;          pref = context.getsharedpreferences(pref_name,private_mode);          editor = pref.edit();     }      public void setfunction(string data) {          editor.putstring(key_name, data);     } } 

in shared preferences make function fetch stored data , view every time in textfield

public class mypreferences {  private sharedpreferences pref;  // editor shared preferences private sharedpreferences.editor editor;  // context private context context;  // shared pref mode int private_mode = 0;  // sharedpref file name private static final string pref_name = "mypreferencesname";  public final static string key_name = "key_value";  public mypreferences(context context) {     this.context = context;     pref = context.getsharedpreferences(pref_name, private_mode);     editor = pref.edit(); }  public void setfunction(string data) {     editor.putstring(key_name, data);     editor.commit(); }  public string getfunction() {     return pref.getstring(key_name, ""); } } 

and in activity initialize sharedpreferences , following:

private mypreferences mypreferences; @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      mypreferences = new mypreferences(this);      //to set edittext data savedpreferences     textfield.settext(mypreferences.getfunction()); }  //to save latest data edittext. @override protected void onstop() {     log.d("lifecycle", "onstop: ");     super.onstop();     mypreferences.setfunction(edittext.gettext().tostring()); } 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -