等待多个意图打开错误活动的意图

时间:2016-02-20 16:56:56

标签: android android-widget android-pendingintent

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

  1. 多次点击窗口小部件(A或B),多个实例 同样的活动开启了。
  2. 有时小部件A打开小部件Bs活动,反之亦然。
  3. 这是我的代码:

    Widget A Provider类:

     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);
        }
    

    Widget B Provider类:

     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);
            }
    

2 个答案:

答案 0 :(得分:1)

设置标记为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

答案 1 :(得分:0)

你为什么要打电话

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

2次只调用一次并使用android:launchMode="singleTop"进行多实例问题