I am building an alarm application. I currently have a ListActivity which display the list of alarms and another activity which changes the time of a particular alarm. My problem is, when I decide to enter into the activity to change the time of an alarm and then I press the back button, it does not refresh. However, I've implemented a button that redirects back to the ListActivity and if I press it, the list of alarms are refreshed. How can I, after pressing the back button, refresh the list of alarms?
Asked
Active
Viewed 2.1k times
4
-
onRestart() is better for this! onResume() does the procedure twice, and onRestart() is called when you press the back button, and the procedure is done only once. – Sarah Sakamoto Jan 12 '14 at 08:46
4 Answers
9
You should probably override your onResume() to check if the list of alarms have been refreshed so that whenever you come to/come back to your ListActivity, it'll get updated.

corgichu
- 2,580
- 3
- 32
- 46
7
In your alarm list activity
use
public void onRestart() {
// reload your list items if changed and adapter.notifydatastatechange();
}
If you done this in onResume then its call two times first when your activity start n 2nd when your activity restart.

Vishal Yadav
- 3,642
- 3
- 25
- 42

Dhaval Parmar
- 18,812
- 8
- 82
- 177
-
2I tested using onResume() and when Activity is created, it calls the function (and it is not necessary). But using onRestart() it is only called in the case of the Activity called by back button (and it is what we want). So using onRestart() is much better for this! – Sarah Sakamoto Jan 12 '14 at 08:40
2
here is the code.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent a = new Intent(this,yourback.class);
a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(a);
return true;
}
return super.onKeyDown(keyCode, event);
}
i always use this when back is listview.

Puja Surya
- 1,155
- 4
- 20
- 47
0
I am using following one.
Intent intent = new Intent(CurrentActivity.this, PreviousActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
CurrentActivity.this.finish();

aanshu
- 1,602
- 12
- 13