Android:从小部件打开错误的活动

发布于 2024-12-07 02:45:12 字数 1312 浏览 1 评论 0原文

我已经为这个问题苦苦挣扎了一段时间了。我有一个小部件,显示烹饪食谱的摘要。该小部件是可单击的,因此每当用户单击它时,都会打开新活动,其中包含该食谱的完整描述。问题是,有时当单击小部件时,它会打开错误的活动。让我举个例子:

  1. 用户单击小部件,打开活动 A
  2. 用户从活动 A 启动活动 B(例如配方索引)。
  3. 用户从活动 B 启动活动 C。
  4. 用户按下主页按钮。
  5. 用户再次单击小部件,显示活动 C 而不是 A

我尝试设置这三个标志的组合,这仅有助于解决一半的问题:

openIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

通过使用上述标志,我设法显示正确的活动(A),直到我执行以下操作:

  1. 用户从小部件打开活动 A
  2. 用户移动到活动 B 并通过单击新配方打开活动 A
  3. 新配方将显示给用户
  4. 用户单击“主页”按钮并单击小部件。
  5. 显示活动 A(右侧),但配方不同。
  6. 从现在开始,如果我移动到不同的活动 B 或 C 并单击“主页”,然后再次小部件,则不会显示活动 A,而是显示按“主页”按钮之前可见的活动。

我希望我解释得足够详细。我真的很感激你的帮助,因为这件事让我发疯!

更新:

这是我的待定意图:

Intent openIntent = new Intent(context, ProfanityDefinition.class);

openIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
openIntent.setData(Uri.parse(openIntent.toUri(Intent.URI_INTENT_SCHEME)));

Bundle bundle = new Bundle();
bundle.putBoolean("widgetRequest", true);
openIntent.putExtras(bundle);
PendingIntent openPendingIntent = PendingIntent.getActivity(context, 0, openIntent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.widget_content, openPendingIntent);

谢谢

I have been struggling with this problem for a while now. I have a widget which displays summary of a cooking recipe. The widget is clickable so whenever user clicks on it the new activity is opened with a full description of that recipe. The problem is that sometimes when the widget is clicked it opens a wrong activity. Let me give you an example:

  1. User clicks on widget and activity A is opened
  2. User starts activity B (e.g. recipe index) from the activity A.
  3. User starts activity C from activity B.
  4. User presses Home button.
  5. User clicks on the widget again and the activity C is displayed instead of A

I tried setting a compibation of those three flags which helps in solving only a half of the problem:

openIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

By using above flags I managed to display a correct activity (A) until I do the following:

  1. User opens activity A from the widget
  2. User moves to activity B and opens activity A by clicking on new recipe
  3. New recipe is displayed to the user
  4. User clicks Home button and clicks on the widget.
  5. Activity A is displayed (the right one) but with different recipe.
  6. From now on if I move to different activities B or C and click Home and then widget again the activity A is not displayed but the one which was visible before pressing Home button.

I hope I explained it in enough details. I would be really grateful for your help because this thing drives me crazy!

Updated:

This is my pendingIntent:

Intent openIntent = new Intent(context, ProfanityDefinition.class);

openIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
openIntent.setData(Uri.parse(openIntent.toUri(Intent.URI_INTENT_SCHEME)));

Bundle bundle = new Bundle();
bundle.putBoolean("widgetRequest", true);
openIntent.putExtras(bundle);
PendingIntent openPendingIntent = PendingIntent.getActivity(context, 0, openIntent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.widget_content, openPendingIntent);

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多情出卖 2024-12-14 02:45:12

显示用于创建 PedningIntent 的代码。如果您将额外内容传递给您的活动,请注意,仅额外内容不同的两个意图将被视为等效。您可能希望在创建 PendingIntent 时传递 PendingIntent.FLAG_CANCEL_CURRENT 以确保您的意图不会被重用。

Show the code for creating the PedningIntent. If you are passing extras to your activity, be aware that two intents that differ only by extras are treated as equivalent. You probably want to pass the PendingIntent.FLAG_CANCEL_CURRENT when you create the PendingIntent to make sure your intent is not reused.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文