什么时候需要使用singleTop launchMode在Android窗口小部件或应用程序? [英] When is it necessary to use singleTop launchMode in an android widget or application?

查看:365
本文介绍了什么时候需要使用singleTop launchMode在Android窗口小部件或应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚烧毁一个小部件。我可以看到这个问题的原因,但我不能确定为什么及其解决方案。我的窗口小部件正发出一个搜索(SearchManager)和活动展开了搜索对话框,但是当它叫回我的小部件,它创造了另一个参照小部件(即线程ID是一样的,但小部件ID从65变更为0)。

这使我相信,一个新的实例中得到创建和我搜索的设置将适用于该问题的文件。最后,我偶然发现了安卓launchMode =singleTop键,只要我在AndroidManifest设置,中提琴!我的窗口小部件的工作。

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

是否有任何其他情况下,还是有更多的技术,正确的答案我的问题?

解决方案

我没有更多的阅读在Android的文档 - 我可以花一辈子阅读他们的文档,并找到一个新的微妙的细节,打破我的大脑:)这说明我多-instances是我没想到的,不过,我配置的发生。

Android的文档上的Andr​​oidManifest.xml <活动定义/ P>

  

标准和singleTop模式   彼此在短短的一个不同   尊重:每当有新的打算   为一个标准的活性,一个新的   该类的实例被创建到   应对这一意图。每个实例   处理一个意图。同样地,一   一个singleTop活动的新实例   也可以被创建来处理一个新的   意图。然而,如果目标任务   已经拥有的现有实例   在它的栈的顶部的活性,   该实例将获得新的   意图(在onNewIntent()调用);一个   不会创建新实例。其他   情况下 - 例如,如果一个   现存的singleTop的实例   活动是在目标的任务,但   未在堆栈的顶部,或者如果   它是在一个堆栈的顶部,而不是在   目标任务 - 一个新的实例会   创建并压入堆栈。

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?

解决方案

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.

这篇关于什么时候需要使用singleTop launchMode在Android窗口小部件或应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆