How to open dialog styled activity from notification without previous activity closing?

别说谁变了你拦得住时间么 提交于 2020-02-22 05:44:48

问题


  1. I have an Activity with dialog style so it visually opens on top of previous activity.
  2. I have a notification which opens this activity like this:
Intent intent = new Intent(this, CalcActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
((NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE)).notify(0, builder.build());

The problem is that when user taps my notification, previous visible activity closes, home screen appears and my dialog is displayed on top of home screen. How to prevent closing of previous activity? Please note that previous activity might not come from my app.


回答1:


Use Intent as below :

Intent intent = new Intent(this,MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

and in AndroidManifest.xml register activity and use theme as dialog:

<activity android:name=".MainActivity" android:theme="@style/Theme.AppCompat.Light.Dialog"/>


来源:https://stackoverflow.com/questions/15968559/how-to-open-dialog-styled-activity-from-notification-without-previous-activity-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!