Android 我如何知道活动是否在单击通知后打开?
我有一个显示列表的主活动。但是当用户单击状态栏中的通知时,我想在MainActivity顶部显示一个对话框。 是否可以查询活动是否通过通知启动 我还尝试在意图中添加额外信息,让活动知道应该弹出该对话框。但是bundle总是空的。将此添加到onResume()没有意义,因为活动可能已经可见Android 我如何知道活动是否在单击通知后打开?,android,android-intent,android-activity,Android,Android Intent,Android Activity,我有一个显示列表的主活动。但是当用户单击状态栏中的通知时,我想在MainActivity顶部显示一个对话框。 是否可以查询活动是否通过通知启动 我还尝试在意图中添加额外信息,让活动知道应该弹出该对话框。但是bundle总是空的。将此添加到onResume()没有意义,因为活动可能已经可见 long id = intent.getLongExtra("id", 0); String title = intent.getStringExtra("msg"); Notific
long id = intent.getLongExtra("id", 0);
String title = intent.getStringExtra("msg");
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setSmallIcon(R.drawable.ic_action_ic_action_edit);
mBuilder.setContentTitle("EasyReminder");
mBuilder.setContentText(title);
Intent resultIntent = new Intent(context, MainActivity.class);
resultIntent.setAction(Intent.ACTION_MAIN);
resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resultIntent.putExtra("ShowDialog", true);
PendingIntent pi = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pi);
NotificationManager mNotificationManager =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify((int)id, mBuilder.build());
MainActivity.java
@Override
public void onResume() {
super.onResume();
// this is always null
Bundle extras = getIntent().getExtras();
if(extras != null) {
DialogPopupFragment fr = new DialogPopupFragment();
fr.show(getFragmentManager(), "DialogPopupFragment");
}
}
尝试在resultIntent上设置flag flag\u ACTIVITY\u SINGLE\u TOP,并实现方法 MainActivity中的onNewIntent()。您可以通过活动生命周期回调方法了解