java - passing EditText content by SharedPreference -


in application have 2 activities , want type url address in edittext of settingsactivity , use in mainactivity can't ...

this settingsactivity :

 public class settingsactivity extends activity {  switch aswitch; edittext edittext; string url_endpoint;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_settings);      edittext = (edittext) findviewbyid(r.id.etend);     sharedpreferences sp = getsharedpreferences("edittext", activity.mode_private);     sharedpreferences.editor edt = sp.edit();     string urlvalue = edittext.gettext().tostring();     edt.putstring("url", urlvalue);     edt.commit();     edittext.settext(sp.getstring("url" , urlvalue)); } 

and mainactivity :

 public class mainactivity extends activity {   string url_endpoint;    @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_monitoring);       //get url endpoint settingsactivity     sharedpreferences sp = getsharedpreferences("edittext", activity.mode_private);     string url = sp.getstring("url", "");     url_endpoint = url; 

first, have @ android's lifecycle

enter image description here

now, said, oncreate method called once when activity created, can see in picture.

for reason, getting text of edittext in oncreate method result in empty text (if not pre-populated).

since think want create settings activity set stuffs mainactivity, have 2 ways achieving goal:

  1. you can add "save" button. reccomend since user might input , change mind, , way clicking "back" cancel operation.
  2. you can save edittext value @ onpause of settingsactivity. method override value every time user exits activity no matter what.

note: remember popolate text of settingsactivity in oncreate/onresume, or empty.

also point that, can see in picture, once come settings activity oncreate not called again. have, in onresume method, take value sharedpreference , put in mainactivity's textview.

first, reccomend study lifecycle because it's core information programming android.


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -