0

I have an onPause and an onResume, see the two:

@Override
public void onResume(){
    super.onResume();
    String str = "";

    SharedPreferences prefs = this.getSharedPreferences(
            "com.example.app", Context.MODE_PRIVATE);
    for (int i = 0; i<prefs.getAll().size(); i++){
        str = prefs.getString(String.valueOf(i), "");
        meds.add(str);
        adap.add(str);
        adap.notifyDataSetChanged();}}


  @Override
public void onPause() {
    super.onPause();
    SharedPreferences prefs = this.getSharedPreferences(
            "com.example.app", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    for (int i = 0; i<meds.size(); i++){
        editor.putString(String.valueOf(i), meds.get(i));
        String str1 = editor.toString();
        String str = prefs.getString("0", "");

    }
    editor.commit();
}

Now this works fine whenever I really want to resume the activity -say, the user presses the backbutton.

However, when I completely restart the app (as in: shut down the program, run again), apparently onResume is called because my Data are set to those saved in onPause.

Am I just using the wrong method? When actually restarting the app, I want to start with my default data and not with what is saved in onPause.

user1862770
  • 307
  • 1
  • 4
  • 13
  • 3
    See [Activity LifeCycle](http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle) `OnResume` is always called whenever you launch a new activity. – Shobhit Puri Dec 01 '14 at 19:05
  • Hm. So if I do not want to have my data when restarting the app I have to destroy them in the onDestroy? because onDestroy is called when the program stops? – user1862770 Dec 01 '14 at 19:08
  • 2
    Actually [onDestroy](http://developer.android.com/reference/android/app/Activity.html#onDestroy%28%29) is not reliable and might not be called in some conditions like when OS kills your activity due to memory requirements. – Shobhit Puri Dec 01 '14 at 19:15
  • So I know now what I am doing wrong but not how to do it correctly. Is there a variable or sth. which I can call in onCreate to check if the activity starts for the first time during the running of the app? – user1862770 Dec 01 '14 at 19:18

2 Answers2

1

OnResume is always called whenever you launch a new activity. You can see this from Activity LifeCycle. If you do not want to have your data when restarting the app, use isFinishing() method inside onPause and if it returns true clear everything before closing the Activity.

isFinishing() checks whether this activity is in the process of finishing, either because you called finish() on it or someone else has requested that it finished.

Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • Thank you, just testing. I suppose this does not work when I simply shut down the emulator or the phone, does it? If there is no time to call the method.... – user1862770 Dec 01 '14 at 19:21
  • 1
    You tried shutting down the phone when you are still on the activity? It doesn't call `onPause` ? – Shobhit Puri Dec 01 '14 at 19:32
  • Yes I tried that and you are, of course, correct, onPause is not called, write that as an answer and I will accept it. My bad - I should have overriden onBackPressed instead of onPause in the first place. So what should I do with this question now, what would be acceptable on stackoverflow? – user1862770 Dec 01 '14 at 19:44
0
  1. in onCreate() set the default values .
  2. in OnResume() get the latest values and update the the values of meds and adap
  3. in OnPause() save your preferences.. that is whatever you are doing now..
  4. you need not to clear the preferences them in onDestroy() , since there is no guarantee that the system call onDestroy()
Sreekanth
  • 21
  • 1
  • 6
  • ahm.....that is EXACTLY what I am doing now. What happens then is: app starts, Activity runs onCreate, sets defaultvalues. THEN runs onResume, updates my Values. and THAT is exactly what I do not want. – user1862770 Dec 01 '14 at 19:35
  • since you don't need to maintain the previous app state ..don't use preferences at all , instead use some member variable to temporarily store your values, when the activity switches from onPause() to onResume() .hopes this suits your requirement :) – Sreekanth Dec 01 '14 at 19:45