android - onCreate being called on Activity A in up navigation -
so have activity , activity b. want activity able navigate activity b press of button. works, when use navigation(the home button in action bar) navigate activity a, oncreate() called again , old information user typed in lost.
i've seen: oncreate called if navigating intent, used fragments, , i'm hoping not have redesign entire app use fragments. there way can stop oncreate() being called every time activity becomes active again?
this behavior totally fine , wanted. system might decide stop activities
in background free memory. same thing happens, when e.g. rotating device.
normally save instance state (like entered text , stuff) bundle , fetch these values bundle when activity
recreated.
here standard code use:
private edittext msomeuserinput; private int msomeexamplefield; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // todo inflate layout , stuff msomeuserinput = (edittext) findviewbyid(r.id.some_view_id); if (savedinstancestate == null) { // todo instanciate default values msomeexamplefield = 42; } else { // todo read instance state savedinstancestate // , set values views , private fields msomeuserinput.settext(savedinstancestate.getstring("msomeuserinput")); msomeexamplefield = savedinstancestate.getint("msomeexamplefield"); } } @override protected void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); // todo save instance outstate outstate.putstring("msomeuserinput", msomeuserinput.gettext().tostring()); outstate.putint("msomeexamplefield", msomeexamplefield); }
Comments
Post a Comment