我有两个小部件A和B.我有两个问题:
这是我的代码:
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);
}
答案 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"
进行多实例问题