Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android启动相同的活动,但关闭上一个_Android_Android Activity_Android Notifications - Fatal编程技术网

Android启动相同的活动,但关闭上一个

Android启动相同的活动,但关闭上一个,android,android-activity,android-notifications,Android,Android Activity,Android Notifications,我正试图从通知启动我的活动,它正在工作,但不是启动同一个活动,而是在我退出时启动另一个(副本)我的活动,我在它下面找到另一个活动副本 NotificationCompat.Builder b = new NotificationCompat.Builder(this); .... Intent intent = new Intent(this, MyActivity.class); intent.putExtra("item", currentObj); PendingIntent pInten

我正试图从通知启动我的活动,它正在工作,但不是启动同一个活动,而是在我退出时启动另一个(副本)我的活动,我在它下面找到另一个活动副本

NotificationCompat.Builder b = new NotificationCompat.Builder(this);
....
Intent intent = new Intent(this, MyActivity.class);
intent.putExtra("item", currentObj);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
....
b.setContentIntent(pIntent);

如何解决此问题???

您可以调用
finish()之前
b.setContentIntent(pIntent)

您可以调用
finish()之前
b.setContentIntent(pIntent)

您必须使用TaskStackBuilder并添加其父级

  TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);

// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);

stackBuilder.addNextIntent(resultIntent);
您已将其添加到通知中

stackBuilder.addParentStack(MainActivity.class);

它所做的很简单,它只是说android关于您打开的当前类的父类!这会解决你的问题。我希望这是有帮助的。谢谢

您必须使用TaskStackBuilder并添加其父级

  TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);

// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);

stackBuilder.addNextIntent(resultIntent);
您已将其添加到通知中

stackBuilder.addParentStack(MainActivity.class);

它所做的很简单,它只是说android关于您打开的当前类的父类!这会解决你的问题。我希望这是有帮助的。谢谢

您可以设置一个标志,每次启动相同的活动而不是新的活动:

Intent intent = new Intent(this, MyActivity.class);
intent.putExtra("item", currentObj);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);    

您可以设置一个标志,每次启动相同的活动而不是新的活动:

Intent intent = new Intent(this, MyActivity.class);
intent.putExtra("item", currentObj);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);    

您可以将此行添加到
活动中
清单中
以防止多次加载同一活动

android:launchMode = "singleInstance"

在开始这样的活动之前标记意图

intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

您可以将此行添加到
活动中
清单中
以防止多次加载同一活动

android:launchMode = "singleInstance"

在开始这样的活动之前标记意图

intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

如何启动活动取决于后台堆栈和启动模式。您应该阅读android back stack和manifest file attribute
launchMode
以了解更多信息。您好,您解决了这个问题了吗,或者仍然存在一些问题……如何启动活动取决于back stack和启动模式。您应该阅读android back stack和manifest file attribute
launchMode
以了解更多信息。嗨,您解决了这个问题了吗,或者仍然存在一些问题……这不会启动新活动,如果已经存在,它会打开旧活动。这不会启动新活动,如果已经存在,它会打开旧活动。