1

Is anyone aware of the reason why my onDestroy method to save a bool inside my shared pref works fine on vd but doesn't always work on real device? it looks like working when i quit the app and go back further to main screen but not when i quit the app, remain in the app list window and open the app back again.. weird is there some bug i'm not aware of or may depend on my other classes? if so i can post the code but is very simple, just as said a bool save and a get

edit: if you guys try to create a scrollview in a fragment (frag of a MainActivity), and try to go on edit mode and press back button to quit your app, mess with it a few times and you'll see onDestroy and even onStop won't be called.

Q. So how am I supposed to save my pref before quitting an app in a consistent way? reliable all the times? also for this i'd need an onDestroy alternative not even onStop, cause I need to save pref only before app quit not on activity change, and since onDestroy isn't reliable (from what Android Dev guide says) what should I use?? thanks

WORKAROUND: I placed this 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 is boolean firstVisit; declared at start. (this boolean trick was part of a solution from another post, but it didn't work well saved inside onDestroy() or onStop() cause sometimes they don't get called)

then I added this inside my 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 it works, what it basically does is: set my scrollview to be hidden everytime my app starts (through savedInstanceState as @bwt suggested below), and remains hidden when i pass to another activity and come back (using bool = true) unless i press the button inside first activity (setting and saving bool == false) so from then on the scrollview is always visible even if i pass to another activity and come back

m4tt
  • 825
  • 1
  • 11
  • 28
  • There is no limit with SharedPreferences xml. Do you add apply() or commit() to your editor? Give us some code to see. – Stanojkovic Jan 28 '16 at 13:35
  • yea or it wouldn't work on vd either – m4tt Jan 28 '16 at 13:40
  • 1
    Place your code here. – Stanojkovic Jan 28 '16 at 13:42
  • use log to determine whether onDestroy being called? – Farhan Jan 28 '16 at 13:48
  • Possible duplicate of [Android Activity onDestroy() is not always called and if called only part of the code is executed](http://stackoverflow.com/questions/18361719/android-activity-ondestroy-is-not-always-called-and-if-called-only-part-of-the) – Yazan Jan 28 '16 at 14:07
  • there is no guarantee when onDestroy is called, your VD **might** have less memory than the real device so it calls onDestroy more frequant ?! – Yazan Jan 28 '16 at 14:08
  • placed Logs inside fragment's onDetach() onDestroy() and inside onDestroy() of it's root activity. They being called randomly.. when they get called all 3 all works fine going back to the app, when none get called (most of the times) or only an onDestroy() from fragment get called it doesn't work – m4tt Jan 28 '16 at 14:17
  • funny thing is that even onStop get called only certain times, not all the times – m4tt Jan 28 '16 at 14:32
  • Have you read http://developer.android.com/reference/android/app/Activity.html especially around the lifecycle diagram? – Gavriel Jan 28 '16 at 15:01
  • @Gavirel yes i do, but when onStop and onDestroy get called randomly how am I supposed to save my pref before quitting an app? also i'd need an onDestroy alternative not even onStop, cause I need to save pref only before app quit not on activity change, and since onDestroy isn't reliable (from what Android Dev guide says) what should I use?? – m4tt Jan 28 '16 at 15:21

1 Answers1

1

I think the right moment to save a preference is when it changes, not when the application quits. Specially since Android may choose to simply kill a process (and the included application) without calling any callback. Use apply() instead of commit(), so that the application does not have to wait.

Alternatively you can override onSaveInstanceState(). It is not part of the basic lifecycle but gives you the opportunity to store the current state of your Activity before being destroyed.

bwt
  • 17,292
  • 1
  • 42
  • 60
  • so if i want my scrollview to be hidden everytime my app starts, and remains hidden when i pass to another activity and come back unless i press a button inside first activity so from then on the scrollview is always visible even if i pass to another activity and come back, how should i use onSaveInstanceState() or boolean inside a shared pref? – m4tt Jan 28 '16 at 17:14
  • I would 1) in the first activity, save the current scrollview state in the bundle provided by `onSaveInstanceState()`, 2) when the user goes back to this activity, either the activity has been destroyed, is recreated and you can retrieve the scrollview state in the bundle provided to `onCreate()` (which is not null precisely in this case), or the activity has not been destroyed and you don't need to restore the state – bwt Jan 28 '16 at 17:52
  • solved with your suggestion of savedInstanceState and the bools i had already. thanks for your help! (edited with solution) – m4tt Jan 28 '16 at 18:01