android - 多个 Intent 的未决 Intent 打开错误的 Activity

标签 android android-widget android-pendingintent

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

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

这是我的代码:

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

小部件 B 提供者类:

 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

并尝试将其添加到 list 中。

android:launchMode="singleTask"

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

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

关于android - 多个 Intent 的未决 Intent 打开错误的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35526212/

相关文章:

android - 首次使用android studio "URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs)"错误

android - 使用 https ://fcm. googleapis.com/fcm/send 发送 Firebase 数据消息时注册无效?

android - 将 PendingIntent 中的小部件的额外内容传递给 Activity

Android ImageView 的 Widget 动画

android - 如何从 pendingintent 中接收额外信息?

java - AlarmManager 并不总是执行

安卓 : Turn on a device programmatically

android - 在没有日期选择器的情况下使用 DatePicker

android - 多个 Intent ,但广播接收器仅调用一次

Android EditText 粘贴文本时如何禁用自动插入空格?