Unexpected behavior when launching a dialog-style activity

I observed from WhatsApp such behaviors: when a new message arrives, WhatsApp will launch a dialog-style activity and such a activity could be cancelled or confirmed by the user.

Therefore, I added a similar dialog-style activity to my app. For test purposes, I also added an alarm manager and an alarm receiver so that this activity will be launched every 10 seconds by the alarm receiver.

The code I used to launch the dialog-style activity is:

Intent dialogIntent = new Intent(MyApplication.sharedApplication, MyDialogStyleActivity.class); dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); MyApplication.sharedApplication.startActivity(dialogIntent);

Everything looked fine if my application had not been launched. The dialog activity would show , behind which was the home screen, just like WhatsApp's behavior.

But there was a problem, if my application had been launched and put in the background, and then the alarm triggered the launch of the dialog activity, before the launch of the dialog activity, the main activity of my app would come back to foreground and show as well.

I'm confused, all I want for now is that, even if my application had been launched, when something triggers the dialog-style activity, only this dialog-style activity would show, no main activity or any other un-related activity.

Anyone could help?


Your application maintains an activity stack. So your main activity A and your dialog activity B is represented as A->B in your app's activity stack.

When your app is launched and put to background. When your dialog activity is invoked and brought to the foreground, your main activity A is still live and thus will be shown as well. Maybe you can call "finish()" in A after startActivity(dialogActivity) to make sure A is finished and goes away.

人吐槽 人点赞



用户名: 密码:
验证码: 匿名发表


查看评论:Unexpected behavior when launching a dialog-style activity