问题
- I have an Activity with dialog style so it visually opens on top of previous activity.
- 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