什么时候需要在 android 小部件或应用程序中使用 singleTop launchMode?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3268962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When is it necessary to use singleTop launchMode in an android widget or application?
提问by mobibob
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).
我刚刚被一个小部件烧伤了。我可以看到问题的原因,但我无法确定原因或解决方案。我的小部件正在发出搜索 (SearchManager) 并且活动启动了一个搜索对话框,但是当它回调到我的小部件时,它创建了对小部件的另一个引用(即,线程 ID 是相同的,但小部件 ID从 65 变为 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.
这让我相信正在创建一个新实例,我在文档中搜索了适用于该问题的设置。最终,android:launchMode="singleTop"
当我在 AndroidManifest 中设置它时,我偶然发现了中提琴!我的小部件工作了。
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?
是否还有其他情况或对我的问题有更技术上更正确的答案?
回答by mobibob
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 Docs 中做了更多的阅读——我可以花一生的时间阅读他们的文档,并找到一个新的微妙细节,让我的大脑崩溃:) 这解释了我没想到的多个实例,但是,我配置为发生。
Android Doc on Activity definition for AndroidManifest.xml
AndroidManifest.xml 活动定义的 Android 文档
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.
“标准”和“单顶”模式在一个方面彼此不同:每次“标准”活动有新的意图时,都会创建一个类的新实例来响应该意图。每个实例处理一个意图。类似地,也可以创建“singleTop”活动的新实例来处理新意图。但是,如果目标任务在其堆栈顶部已经有 Activity 的现有实例,则该实例将接收新的意图(在 onNewIntent() 调用中);不会创建新实例。在其他情况下——例如,如果“singleTop”活动的现有实例在目标任务中,但不在栈顶,或者如果它在栈顶,