为什么通知会打开另一个活动实例?

enz*_*m83 0 android android-service android-notifications

我有一项通过按钮开始服务的活动.在服务初始化期间,如果发生错误,通知会显示此错误,如下所示:

String tickerText = getString(R.string.init_error);

Notification notification = new Notification(
        R.drawable.ic_stat_notify_msg,
        tickerText, System.currentTimeMillis());

PendingIntent notificationIntent = PendingIntent.getActivity(
        this, 0, new Intent(this, MyServiceActivity.class), 0);

notification.setLatestEventInfo(this,
        getText(R.string.init_error_tts_title),
        getText(R.string.init_error_tts_text),
        notificationIntent);

NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
nm.notify(R.string.init_error, notification);

stopSelf();
Run Code Online (Sandbox Code Playgroud)

当通知出现在状态栏中时,我打开通知窗口.然后,当我点击通知时,会启动一个新的MyServiceActivity实例......为什么?

Chr*_*CVB 5

尝试在清单上的活动代码中添加此属性:

android:launchMode=["multiple" | "singleTop" |
                    "singleTask" | "singleInstance"]
Run Code Online (Sandbox Code Playgroud)

用价值singleTopsingleInstance

活动元素文档