何时需要在 Android 小部件或应用程序中使用 singleTop launchMode?
我刚刚被一个小部件烧伤了。我可以看到问题的原因,但无法确定原因或解决方案。我的小部件正在发出搜索(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在 Android Docs 中进行了更多阅读——我可以花一辈子的时间阅读他们的文档,并找到一个让我大吃一惊的新微妙细节:) 这解释了我没想到的多个实例,但是,我配置了发生。
AndroidManifest.xml 的 Activity 定义的 Android 文档
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
意味着如果标记为“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.