sharedpreferences - android onDestroy works fine only on vd -


is aware of reason why ondestroy method save bool inside shared pref works fine on vd doesn't work on real device? looks working when quit app , go further main screen not when quit app, remain in app list window , open app again.. weird there bug i'm not aware of or may depend on other classes? if can post code simple, said bool save , get

edit: if guys try create scrollview in fragment (frag of mainactivity), , try go on edit mode , press button quit app, mess few times , you'll see ondestroy , onstop won't called.

q. how supposed save pref before quitting app in consistent way? reliable times? i'd need ondestroy alternative not onstop, cause need save pref before app quit not on activity change, , since ondestroy isn't reliable (from android dev guide says) should use?? thanks

workaround: placed in oncreateview()

boolpref = getactivity().getsharedpreferences("pref_bool", context.mode_private); firstvisit = boolpref.getboolean("bool", default);  if (firstvisit == false) {   scrollview.setvisibility(view.visible); }else{   scrollview.setvisibility(view.invisible); }  if (savedinstancestate == null) {   scrollview.setvisibility(view.invisible); } 

where firstvisit boolean firstvisit; declared @ start. (this boolean trick part of solution post, didn't work saved inside ondestroy() or onstop() cause don't called)

then added inside button onclick:

      boolpref = getactivity().getsharedpreferences("pref_bool", context.mode_private);       sharedpreferences.editor editor_bool = boolpref.edit();       editor_bool.putboolean("bool", false);       editor_bool.apply();        scrollview.setvisibility(view.visible); 

and works, is: set scrollview hidden everytime app starts (through savedinstancestate @bwt suggested below), , remains hidden when pass activity , come (using bool = true) unless press button inside first activity (setting , saving bool == false) on scrollview visible if pass activity , come back

i think right moment save preference when changes, not when application quits. specially since android may choose kill process (and included application) without calling callback. use apply() instead of commit(), application not have wait.

alternatively can override onsaveinstancestate(). not part of basic lifecycle gives opportunity store current state of activity before being destroyed.


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -