何时需要在 Android 小部件或应用程序中使用 singleTop launchMode?

发布于 2024-09-10 09:41:58 字数 337 浏览 3 评论 0原文

我刚刚被一个小部件烧伤了。我可以看到问题的原因,但无法确定原因或解决方案。我的小部件正在发出搜索(SearchManager)并且活动启动了一个搜索对话框,但是当它回调我的小部件时,它创建了对该小部件的另一个引用(即,线程ID是相同的,但是小部件ID从 65 更改为 0)。

这让我相信正在创建一个新实例,并且我在文档中搜索了适用于该问题的设置。最终,我偶然发现了 android:launchMode="singleTop",当我在 AndroidManifest 中设置它时,中提琴!我的小部件工作了。

这花了我两天的时间来调试。

还有其他情况或者对我的问题有技术上更正确的答案吗?

I just got burned by a widget. I could see the cause of the problem, but I could not determine why or its solution. My widget was issuing a search (SearchManager) and the activity launched a search dialog, but when it called-back to my widget, it created another reference to the widget (i.e., the thread-id was the same, but the widget-id changed from 65 to 0).

This led me to believe that a new instance was getting created and I searched the documentation for settings that would apply to the problem. Eventually, I stumbled upon the android:launchMode="singleTop" and as soon as I set it in the AndroidManifest, viola! My widget worked.

This took me the better part of two days to debug.

Are there any other situations or is there a more technically-correct answer to my problem?

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

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

发布评论

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

评论(2

酒中人 2024-09-17 09:41:58

我在 Android Docs 中进行了更多阅读——我可以花一辈子的时间阅读他们的文档,并找到一个让我大吃一惊的新微妙细节:) 这解释了我没想到的多个实例,但是,我配置了发生。

AndroidManifest.xml 的 Activity 定义的 Android 文档

“标准”和“singleTop”模式
彼此只有一处不同
尊重:每次有新意图时
对于“标准”活动,一个新的
该类的实例被创建为
回应这个意图。每个实例
处理单一意图。同样,一个
“singleTop”活动的新实例
也可以创建来处理新的
意图。然而,如果目标任务
已经有一个现有实例
堆栈顶部的活动,
该实例将收到新的
意图(在 onNewIntent() 调用中);一个
未创建新实例。在其他方面
情况——例如,如果
“singleTop”的现有实例
活动在目标任务中,但是
不在堆栈顶部,或者如果
它位于堆栈的顶部,但不在堆栈中
目标任务 - 一个新实例将
被创建并压入堆栈。

I did more reading in Android Docs -- I could spend a lifetime reading their docs and find a new subtle detail that breaks my brain :) This explains my multiple-instances that I did not expect, however, I configured to occur.

Android Doc on Activity definition for AndroidManifest.xml

The "standard" and "singleTop" modes
differ from each other in just one
respect: Every time there's new intent
for a "standard" activity, a new
instance of the class is created to
respond to that intent. Each instance
handles a single intent. Similarly, a
new instance of a "singleTop" activity
may also be created to handle a new
intent. However, if the target task
already has an existing instance of
the activity at the top of its stack,
that instance will receive the new
intent (in an onNewIntent() call); a
new instance is not created. In other
circumstances — for example, if an
existing instance of the "singleTop"
activity is in the target task, but
not at the top of the stack, or if
it's at the top of a stack, but not in
the target task — a new instance would
be created and pushed on the stack.

暗恋未遂 2024-09-17 09:41:58

意味着如果标记为“singleTop”的活动/实例不在堆栈顶部,并且您尝试调用它,则会创建一个新实例并将其放在堆栈顶部。在标准模式下,您可以随时创建实例,而无需查看堆栈。

Means that if the activity/instance that's been flagged with "singleTop" is not on the top of the stack and you attempt to call it, a new instance will be created and put on top of the stack. In standard mode, you create instances all the time without looking at the stack.

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