android - How to maintain the state of the activity? -
i trying save state of activity in android.basic scenario there 2 activities , b.activity contains 2 edittext fields.user enters value , moves activity b via intent.when user comes activity (by intent have provided button) need display values in 2 edittext fields user had entered (i.e maintain state of activity a).also not want use shared preferences or make fields static. have used following code not help:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.manual_entry); edittext1=(edittext)findviewbyid(r.id.edittext1); } @override protected void onsaveinstancestate(bundle savedinstancestate) { super.onsaveinstancestate(savedinstancestate); savedinstancestate.putstring("cardvalue_saved_inst", value_card_manuallyentered); } @override public void onrestoreinstancestate(bundle savedinstancestate) { super.onrestoreinstancestate(savedinstancestate); value_savedinstance = savedinstancestate.getstring("cardvalue_saved_inst"); log.e("value_savedinstance",""+value_savedinstance); edittext1.settext(value_savedinstance); }
in activitya
, if launch activityb
, don't call finish()
on yourself, when activityb
finishes (when user presses button or otherwise), activitya
shown in same state in. standard android behaviour , don't need special.
onrestoreinstancestate()
not called if paused activity gets resumed (which usual case activitya
launches activityb
, activityb
finishes).
onrestoreinstancestate()
only called if android killed activity (during orientation change example) or killed process. in case, when user returns activity, android create new instance of activity , call onrestoreinstancestate()
.
Comments
Post a Comment