Android:从小部件打开错误的活动
我已经为这个问题苦苦挣扎了一段时间了。我有一个小部件,显示烹饪食谱的摘要。该小部件是可单击的,因此每当用户单击它时,都会打开新活动,其中包含该食谱的完整描述。问题是,有时当单击小部件时,它会打开错误的活动。让我举个例子:
- 用户单击小部件,打开活动 A
- 用户从活动 A 启动活动 B(例如配方索引)。
- 用户从活动 B 启动活动 C。
- 用户按下主页按钮。
- 用户再次单击小部件,显示活动 C 而不是 A
我尝试设置这三个标志的组合,这仅有助于解决一半的问题:
openIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
通过使用上述标志,我设法显示正确的活动(A),直到我执行以下操作:
- 用户从小部件打开活动 A
- 用户移动到活动 B 并通过单击新配方打开活动 A
- 新配方将显示给用户
- 用户单击“主页”按钮并单击小部件。
- 显示活动 A(右侧),但配方不同。
- 从现在开始,如果我移动到不同的活动 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:
- User clicks on widget and activity A is opened
- User starts activity B (e.g. recipe index) from the activity A.
- User starts activity C from activity B.
- User presses Home button.
- 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:
- User opens activity A from the widget
- User moves to activity B and opens activity A by clicking on new recipe
- New recipe is displayed to the user
- User clicks Home button and clicks on the widget.
- Activity A is displayed (the right one) but with different recipe.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显示用于创建
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 thePendingIntent.FLAG_CANCEL_CURRENT
when you create thePendingIntent
to make sure your intent is not reused.