Take the 2-minute tour ×

I have an application which receives a broadcast from the AlarmManager. Upon this, it starts a transparent Activity (AlarmAlertDialogActivity) which then shows an AlertDialog. Clicking cancel on the AlertDialog result in a call to finish().

As the AlarmAlertDialogActivity is not launched from another Activity but a broadcast receiver, it is launched with


This means the Activity will be launched in a new task.

My problem is that when the app is relaunched from recent history after cancelling the AlertDialog (i.e. by holding the home button and clicking the app's icon) the AlertDialog is relaunched. I had hoped by using finish()/Intent flags I would be able to avoid this; what I would like to happen is the last Activity before the AlertDialog's parent Activity to be launched.

I have tried bitmasking Intent.FLAG_ACTIVITY_NO_HISTORY as an additional flag when launching AlarmAlertDialogActivity but this appears to make no difference.

Bitmasking Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS works, but only by removing the app from the recent history (as the name suggests). This is detrimental to the user experience.

So, is it possible to get the UI flow I am looking for?

UPDATE - more information as requested:

Logcat from Broadcast receiver, the AlertDialog activity and my main activity:

    05-30 10:36:00.132: D/everyOtherApp(362): Received alarm broadcast at: Wed May 30 10:36:00 GMT+00:00 2012
05-30 10:36:00.262: D/everyOtherApp(362): AlarmAlertDialogActivity.onCreate()
05-30 10:36:00.912: D/everyOtherApp(362): AlarmAlertDialogActivity.onResume()
05-30 10:36:12.461: D/everyOtherApp(362): Cancel pressed

//Cancel exits the activity. I now relaunch the app from recent history:

05-30 10:36:20.233: D/everyOtherApp(362): AlarmAlertDialogActivity.onCreate()
05-30 10:36:21.621: D/everyOtherApp(362): AlarmAlertDialogActivity.onResume()

Code for launching Activity from BroadcastReceiver:

        Intent intent = new Intent(new Intent(applicationContext, AlarmAlertDialogActivity.class));
    intent.putExtra(Constants.SCHEDULED_ALARM_TAG, alarm);

AlarmAlertDialogActivity in manfest file:

        android:theme="@android:style/Theme.NoDisplay" >
share|improve this question
"Upon this, it starts a transparent Activity..." - Don't do this! I don't mean the transparent part, I mean don't have an 'alarm' trigger an intrusive UI object (the AlertDialog). By all means use a Notification that will appear in the status bar but don't interrupt anything else a user might be doing. –  Squonk May 28 '12 at 20:52
@MisterSquonk Yes I realise that. The app is very much in alpha stage at the moment and I intend to either remove the intrusive dialog completely or make it a non-default user option, but I would like to know if there is a way to do what I want. –  barry May 28 '12 at 21:08
I don't know if there is a way to do it as it's not something I've ever considered doing. If this is 'alpha' code then, to me, it seems like you're wasting your time and efforts going down this road if you intend to remove it. Even making it 'optional' would mean anyone who found it intrusive would disable it anyway. All I'm saying is step back and rethink what this is supposed to achieve and think of an approach which better fits the UX. –  Squonk May 28 '12 at 21:16
I think you may be missing something here. I can't see any reason why returning to the application from the recent history would cause this behaviour. Please add debug logging to log the calls to onCreate() and onResume() in your various activities and then track when the activities are getting created and resumed. Perhaps that will shed some light on the problem. Also post the relevant parts of the manifest. –  David Wasser May 30 '12 at 8:44
@DavidWasser I've updated the question –  barry May 30 '12 at 10:41
add comment

1 Answer

up vote 7 down vote accepted

I did something similar in another project. I had a BroadcastReceiver that got information about data connectivity and SIM-Profile changes and showed a dialog (using an activity like yours) warning the user that he might incur charges. What I ended up doing was the following:

In the manifest, in the <Activity> tag for your AlarmAlertDialogActivity, add the following:


Explanation: setting excludeFromRecents and noHistory to "true" ensure that the activity won't show up in the list of recent applications and also that once the user navigates away from it he won't be able to go back there (which is probably what you want). Setting taskAffinity to the empty string ensures that AlarmAlertDialogActivity will be run in it's own task even if your application is running when your dialog is shown.

As long as you have another activity that functions as the main activity of your application (ie: with intent filters for action.MAIN and category.LAUNCHER) this should solve your problem.

share|improve this answer
Excellent answer and thanks for the explanation. I'll probably end up using SMS, email or notifications to alert my users, but I'm glad I know how to do this! –  barry May 30 '12 at 15:29
add comment

Your Answer


