I would like to trigger showing a dialog X seconds after something happens in an activity (resumed, button clicked, etc.)
Asked
Active
Viewed 2,553 times
2 Answers
3
use handler.postDelayed(runnable, delayedms);
this is one of the methods, you can also use AlaramManager.
For AlaramManager your activity need not be active. and handler it should be.

Yashwanth Kumar
- 28,931
- 15
- 65
- 69
-
With Handler, how do I verify that the activity is active? With AlarmManager, how do I connect to the correct context (the current active activity) to show the dialog? – orip Oct 23 '11 at 14:31
-
you can use handler.removeCallbacks(runnable) , in your activity onPause() method, which will remove the scheduled operation once your activity is out of focus. As far as the alarm manager is concerned, it is used for notification kind of things, or to do some back ground process, not for your purpose of showing dialog boxes, i think. – Yashwanth Kumar Oct 23 '11 at 14:39
-
+1, thanks! Could my activity be killed without onPause being called, but with the handler still being active? – orip Oct 23 '11 at 15:00
-
no, onPause() is called, before pausing or killing an activity. – Yashwanth Kumar Oct 23 '11 at 15:07
0
One option I considered: instead of attaching a dialog to an existing view, I can create a transparent view (with <activity android:theme="@android:style/Theme.Translucent.NoTitleBar"
as mentioned here) and have it create a dialog.
This is nice since I can create this activity with a delayed intent.
I need to make sure to call finish()
when the dialog is closed.
I'm not sure how to have this happen only if the original activity is still active.