多个意图的未决意图打开错误的活动

Pending intents for multiple intents opening wrong activities

我有两个小部件 A 和 B。我有两个问题:

  1. 多次点击小部件(A 或 B)时,多个实例 同样 activity 打开。
  2. 有时小部件 A 打开小部件 B activity,反之亦然。

这是我的代码:

小部件 A 提供商 class:

 for (int appWidgetId : appWidgetIds) {
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.favorite_flavor_widget);
        Intent intent = new Intent(context, SplashActivity.class);
        intent.setAction("FromFavoriteFlavorWidget");
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
        intent.putExtra("FromFavoriteFlavorWidget", true);
        PendingIntent pendingIntent = PendingIntent.getActivity(context,
                0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.favoriteFlavorBox, pendingIntent);
        appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
        appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
    }

小部件 B 提供商 class:

 for (int appWidgetId : appWidgetIds) {
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                    R.layout.report_food_widget);
            Intent intent = new Intent(context, SplashActivity.class);
            intent.setAction("FromReportFoodWidget");
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
            intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.putExtra("FromReportFoodWidget", true);
            PendingIntent pendingIntent = PendingIntent.getActivity(context,
                    0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            remoteViews.setOnClickPendingIntent(R.id.reportFoodBox, pendingIntent);
            appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
        }

设置标志为Intent.FLAG_ACTIVITY_CLEAR_TOP Intent.FLAG_ACTIVITY_REORDER_TO_FRONT

并尝试将其添加到清单中。

android:launchMode="singleTask"

TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(intent );

http://inthecheesefactory.com/blog/understand-android-activity-launchmode/en

你为什么打电话给

appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
        appWidgetManager.updateAppWidget(appWidgetId, remoteViews);

2 次仅调用一次并使用android:launchMode="singleTop"用于多实例问题