java - Retaining the data in the view during activity switch? -


in android app user can enter text in editview , click on button takes him other activity can select contact ... , press button brings him first activity...

now problem need pass selected contact first activity , display (which have done using bundle) unable retain entered text in editview... should (but text should retained out passing through the bundle , getting back)

thanks :)

the text in view component automagically saved os, after soft kill (user changed phone orientation), not after hard kill, user hit button while parent activity in focus. so, unless doing non-standard, such calling onsaveinstancestate without calling super.onsaveinstancestate, data in view state should persist.

one solution save text in view component non view instance property before launch child activity, , read value when focus returns parent activity in method onactivityresult.

jal

edit: android docs activity page has been extensively updated. view state not saved if widget not have id.

edit: saying view state should persisted os. should not need save view state manually. on hard kill, need save state of activity if expected behavior of activity. here code saves activity state. given instance variable:

string password; 

here save state on soft kill:

   protected void onsaveinstancestate(bundle outstate){         password= edittextpassword.gettext().tostring();         outstate.putstring("password", password);         super.onsaveinstancestate(outstate); // save view state     } 

here save state on hard kill

@override protected void onstop(){    super.onstop();    sharedpreferences prefs = getpreferences(mode_private);   sharedpreferences.editor editor = prefs.edit();   editor.putstring("password",password);    editor.commit(); 

here restore state in oncreate(bundle savedinstancestate):

if( savedinstancestate!= null){ // saved state soft kill after first pass     try {         password= savedinstancestate.getstring("password");         log.d(tag,"restoredstate!");     }     catch(exception e){         log.d(tag,"failedtorestorestate",e);     } } else { // saved state preferences on first pass     sharedpreferences prefs = getpreferences(mode_private); // singleton     if (prefs != null){         this.password= prefs.getstring("password","");             log.d(tag,"gettingprefs");     } } log.d(tag,"oncreate"); 

also given fact if onsaveinstancestate called called before onstop, possible use flags issavedinstancestate , issavedpreferences write prefs on hard kill if reset flags in onresume as:

protected void onresume() {     super.onresume();     log.d(tag,"onresume");     issavedinstancestate= false;     issavedprefs= false; } 

setting flags in oncreate not result in desired outcome.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -