Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.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 按home键后启动不正确的Pending帐篷_Android_Android Pendingintent - Fatal编程技术网

Android 按home键后启动不正确的Pending帐篷

Android 按home键后启动不正确的Pending帐篷,android,android-pendingintent,Android,Android Pendingintent,我有两个(或更多)小部件,例如A和B,它们应该启动相同的活动,并将其Intent中的appWidgetId附加内容传递给活动。在从任何窗口小部件首次启动时安装应用程序后(活动收到正确的appWidgetId),这项工作正常。如果我在活动启动后按back按钮并从另一个小部件启动活动,它也可以正常工作。但是,如果我从小部件A启动活动,点击主页按钮,然后从小部件B(或C或D…)启动活动,它将使用A的appWidgetId启动。我不知道如何解决这个问题。以下是我如何声明我的pendingent: Int

我有两个(或更多)小部件,例如A和B,它们应该启动相同的活动,并将其
Intent
中的
appWidgetId
附加内容传递给活动。在从任何窗口小部件首次启动时安装应用程序后(活动收到正确的
appWidgetId
),这项工作正常。如果我在活动启动后按back按钮并从另一个小部件启动活动,它也可以正常工作。但是,如果我从小部件A启动活动,点击主页按钮,然后从小部件B(或C或D…)启动活动,它将使用A的
appWidgetId
启动。我不知道如何解决这个问题。以下是我如何声明我的
pendingent

Intent intent = new Intent(context, WidgetActivity.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setAction(this.getClass().getName() + System.currentTimeMillis());
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 
                                         PendingIntent.FLAG_UPDATE_CURRENT);
如果我理解正确,我将成功地为每个小部件设置唯一的PendingEvents。非常感谢您的帮助

编辑: 对这一问题的公认答案表明:

我相信问题是你有一个悬挂式帐篷,它的区别只是额外的。PendingEvents是缓存的,因此如果使用两个具有相同操作和数据的内容,它们将相互覆盖


然而,我的代码创建了要执行的随机操作,因此我认为我的挂起内容的差异不仅仅是额外的

看起来我需要重新审视我的活动生命周期事件。答案是在我的活动中加入了以下代码:

@Override
protected void onNewIntent(Intent intent) {
    setIntent(intent);
    super.onNewIntent(intent);
}

问题是,在按下Home按钮时,活动保留了启动它的原始
意图。然后,当另一个小部件启动活动时,原始意图被重用。上面的代码只是用新代码替换了原来的意图,并继续处理剩余的生命周期事件。

@Lars,太棒了!我花了很多时间试图解决这个问题,很高兴看到其他人从我学到的教训中获益:)