草庐IT

android - 具有 singleTask 启动模式的 Activity 及其在后台堆栈中的位置

coder 2023-12-20 原文

在安卓官网上guide on tasks and back stack ,有一条信息我觉得自相矛盾。所以,如果有人能帮我解决这个问题,我将不胜感激。

首先,有以下声明:

Activities in the stack are never rearranged, only pushed and popped from the stack—pushed onto the stack when started by the current activity and popped off when the user leaves it using the Back button.

然后是singleTask启动方式的描述,有如下语句:

The system creates a new task and instantiates the activity at the root of the new task. However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new instance.

好吧,如果我理解正确的话,singleTask Activity 只能是它们任务的根,因为它们是以这种方式创建的,并且 Activity 从不交换后台堆栈中的位置。但是这样的 Activity 在收到对其 onNewIntent() 方法的调用时会如何 react ?

那个页面上还有一个关于此事的评论:

As another example, the Android Browser application declares that the web browser activity should always open in its own task—by specifying the singleTask launch mode in the element. This means that if your application issues an intent to open the Android Browser, its activity is not placed in the same task as your application. Instead, either a new task starts for the Browser or, if the Browser already has a task running in the background, that task is brought forward to handle the new intent.

因此,该指南声明调用 singleTask Activity 以某种方式将其提前。但是,如果该 Activity 不在其任务之上,那怎么可能是真的呢? Activity 是否已恢复,或者它只是收到对其 onNewIntent() 方法的调用而不显示给用户?我真的不明白。

PS:一个几乎相同的question一年多以前就有人问过,但没有给出正确的答案。因此,虽然从技术上讲我的问题是重复的,但我有重新提出它的冲动,因为它确实值得一个清晰明确的答案。

最佳答案

我会尽力回答您的问题。

首先,

Activities in the stack are never rearranged, only pushed and popped from the stack—pushed onto the stack when started by the current activity and popped off when the user leaves it using the Back button.

这其实是错误的。通常,堆栈中的 Activity 永远不会重新排列,但您可以使用 Intent.FLAG_ACTIVITY_REORDER_TO_FRONT 将现有的 Activity 从堆栈中的其他地方移到前面。例如,您的 Activity 堆栈如下所示:A->B->C->D 其中 A 是根 Activity (位于堆栈底部),D是位于前台(显示)的最顶层 Activity 。如果 D 现在这样做:

Intent intent = new Intent(this, B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);

这会将 Activity B 的现有实例移动到任务的前面,这样它就不会显示在屏幕上。 Activity 堆栈现在将如下所示:A->C->D->B。

接下来你问这个:

The system creates a new task and instantiates the activity at the root of the new task. However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new instance.

好吧,如果我理解正确的话,singleTask Activity 只能是它们任务的根,因为它们是以这种方式创建的,并且 Activity 永远不会在后台堆栈中交换位置。但是,此类 Activity 在收到对其 onNewIntent() 方法的调用时会如何 react ?

通常,singleTask Activity 是其任务的根。如果已经有一个包含 singleTask Activity 的现有任务,并且您使用 startActivity() 启动这样的 Activity ,那么该任务将被提前(以便它显示在屏幕上)并且 onNewIntent() 将在该 Activity 的现有实例上调用。 但是,如果 singleTask Activity 已启动其任务中的其他 Activity ,则所有其他 Activity 都将完成(即:任务被剥离回其根 Activity) BEFORE onNewIntent()singleTask Activity 的现有实例上调用。

在某些情况下,singleTask Activity 不是其任务的根,然后所有的赌注都没有了(即:行为没有记录)。如果 singleTask Activity 与应用中已在任务中处于 Activity 状态的其他 Activity 具有相同的 taskAffinity,则可能会发生这种情况。在这种情况下,Android 会忽略 singleTask 启动模式,并将 Activity 视为具有 standard 启动模式。

关于android - 具有 singleTask 启动模式的 Activity 及其在后台堆栈中的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31575072/

有关android - 具有 singleTask 启动模式的 Activity 及其在后台堆栈中的位置的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby - 其他文件中的 Rake 任务 - 2

    我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时

  3. ruby-on-rails - Ruby net/ldap 模块中的内存泄漏 - 2

    作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代

  4. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  5. ruby-on-rails - Rails - 子类化模型的设计模式是什么? - 2

    我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co

  6. ruby - 具有身份验证的私有(private) Ruby Gem 服务器 - 2

    我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..

  7. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  8. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  9. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  10. ruby - 如何在续集中重新加载表模式? - 2

    鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende

随机推荐