简体   繁体   中英

Which Android Intent Flags to Use

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

Intent.FLAG_ACTIVITY_NEW_TASK

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 (ie 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.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra(Constants.SCHEDULED_ALARM_TAG, alarm);
    applicationContext.startActivity(intent);

AlarmAlertDialogActivity in manfest file:

    <activity
        android:name=".AlarmAlertDialogActivity"
        android:theme="@android:style/Theme.NoDisplay" >
    </activity>

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:

android:excludeFromRecents="true"
android:noHistory="true"
android:taskAffinity=""

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM