android - 什么时候需要在 android 小部件或应用程序中使用 singleTop launchMode?

标签 android android-widget android-searchmanager

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

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

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

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

最佳答案

我在 Android Docs 中做了更多的阅读——我可以花一生的时间阅读他们的文档,并找到一个让我脑洞大开的新细节:) 这解释了我没有预料到的多个实例,但是,我配置为发生.

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.

关于android - 什么时候需要在 android 小部件或应用程序中使用 singleTop launchMode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3268962/

相关文章:

android - 改造离线请求和响应

Android:从线程访问ui元素

Android AppWidget如何播放Gif?

android - 在同一 Activity 中在 Android 上执行搜索

Android:saerchable.xml 和 searchSuggestThreshold 参数

java - 安卓辅助搜索 : The search button does not invoke the searchable Activity (Other Solutions did not help)

android - 在 DrawerLayout 中滚动时标题后面的项目列表

android - Android 移动浏览器和混合应用程序不支持自定义古吉拉特语字体 (GopikaTwo)

Android SMS_RECEIVED 广播接收器未被调用

java - 如何在每次选择 Tab 时输出消息?