0

in the onDestroy() method of an activity i want to set some SharedPreferences to the initial value. About a month ago or so this worked. So when the User shuts down the app by clicking the button next to the home button and closing it (don't know how else to describe it) onDestroy() was called and the values were saved - i want them to be saved only when the app is closed totally.

any ideas why this does not work anymore or what to do to solve it?

thank you!

 public void onDestroy() {
    super.onDestroy();
    if(isFinishing()) {
        SharedPreferences gpsPref = getSharedPreferences(getString(R.string.preferences_no_gps), Context.MODE_PRIVATE);
        SharedPreferences.Editor editorGps = gpsPref.edit();
        editorGps.putString(getString(R.string.saved_no_gps), "0");
        editorGps.commit();

        SharedPreferences visibilityPref = getSharedPreferences(getString(R.string.preferences_visibility), Context.MODE_PRIVATE);
        SharedPreferences.Editor editorVisibility = visibilityPref.edit();
        editorVisibility.putString(getString(R.string.saved_visibility), "123");
        editorVisibility.commit();
    }


}

by the way it makes no difference if i put code into if clause or not.

rudi
  • 75
  • 8
  • try to move your code above the super.onDestroy() – malmling Jul 12 '16 at 16:18
  • "So when the User shuts down the app" -- usually users do not shut down apps in Android. Your whole approach is flawed, as `onDestroy()` does not have to be called. However, if you want details of your specific behavior, you need to explain in detail what you mean by "User shuts down the app". – CommonsWare Jul 12 '16 at 16:20
  • tried to describe what i mean ;) – rudi Jul 12 '16 at 16:27
  • You can never know when onDestoy() will be called on an activity. The android system kills an activity arbitrary by calling onDestroy (for example when there is not much memory anymore). You should usually be able to do everything withing onPause. Or maybe you need to explain better your goal... – user3793589 Jul 12 '16 at 17:14
  • If `onDestroy()` wasn't called that means the `Activity` was in background and the system "decided" to kill its instance (or even the whole app's process) while being low on memory. [Android Application class lifecyle documentation](http://stackoverflow.com/questions/35497787/android-application-class-lifecyle-documentation) might give you insight on an application lifecycle. – Onik Jul 12 '16 at 23:39

2 Answers2

1

There is no reliable way to detect it. Because activity did't finished. It's killed. In this case system try to terminate app as fast as possible. Check this answer

Community
  • 1
  • 1
thealeksandr
  • 1,686
  • 12
  • 17
0

my idea

onResume(){
      // below recovery code

     } 
    onPause(){

            SharedPreferences gpsPref = getSharedPreferences(getString(R.string.preferences_no_gps), Context.MODE_PRIVATE);
            SharedPreferences.Editor editorGps = gpsPref.edit();
            editorGps.putString(getString(R.string.saved_no_gps), "0");
            editorGps.commit();

            SharedPreferences visibilityPref = getSharedPreferences(getString(R.string.preferences_visibility), Context.MODE_PRIVATE);
            SharedPreferences.Editor editorVisibility = visibilityPref.edit();
            editorVisibility.putString(getString(R.string.saved_visibility), "123");
            editorVisibility.commit();


    }
loadjang
  • 327
  • 1
  • 3
  • i only wish the values to change if the app is closed and not activity in background :) – rudi Jul 12 '16 at 16:28