android - Going back and forth between intents without losing user entered data -
i have form. here, user fills in details event name, description, location etc. select location, have set button. button code:
btnmap.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub intent map = new intent(getapplicationcontext(), map2.class); startactivity(map); } });
i select location there, , data passed main intent correctly. thing is, data in other fields event, description etc. users filled in lost.
i tried startactivityforresult(map,1);
too. doesn't work. there way keep data, without sending them in bundle other intent , getting them back? select image button does? goes gallery , comes without resetting other fields
contactimageimgview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub intent intent = new intent(); intent.settype("image/*"); intent.setaction(intent.action_get_content); startactivityforresult(intent.createchooser(intent, "select image"), 1); } });
you need save activity state.
override method onsaveinstancestate
, persist data in bundle
in oncreate
, if savedinstancestate
not null, restore data , set fields.
@override public void onsaveinstancestate(bundle savedinstancestate) { savedinstancestate.putint("my_field", 43); // ... other fields super.onsaveinstancestate(savedinstancestate); } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // call superclass first if (savedinstancestate != null) { int value = savedinstancestate.getint("my_field"); // ... update views } else { // no previous state, start fresh } }
details here
note: have mentioned rotation blocked. bad user unless it's part of feature of app.
explanation:
a screen rotation configuration change causes activity restart appropriate resources. there many more configuration changes such screen unlock,device docked etc. not of them trigger activity restart do. when activity restarts due configuration change, onsaveinstancestate
gets called , savedinstancestate
not-null when oncreate
called.
Comments
Post a Comment