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.
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 insideonPause
and if it returnstrue
clear everything before closing theActivity
.