[转]Android Activity的加载模式和onActivityResult方法之间的冲突
前言
今天在调试程序时,发现在某一Activity上点击返回键会调用该Activity的onActivityResult()方法。我一开始用log,后来用断点跟踪调试半天,还是百思不得其解。因为之前其他的Activity的LaunchMode都是Normal,没有特殊设定,这个Activity由于需求改成了singleTop。直到我确定没有一个地方是代码主动触发的,我才想到了跟Activity的LaunchMode是否有关。
探索
在Google上搜索android activity onactivityresult singTop找到了一些问题。
stackoverflow
stackoverflow上有些人跟我遇到的问题类似。比如说有一位开发者把Activity设置成了singleTask模式,onActivityResult就收不到任何结果了。当他把singleTask模式改回标准模式,又恢复正常。
这个问题下面给出的答案中,有一位说startActivityForResult的文档中有这么一句话:
For example, if the activity you are launching uses the singleTask launch mode, it will not run in your task and thus you will immediately receive a cancel result.
意思是:比如说,如果你正加载的activity使用了singleTask的加载模式,它不会在你的栈中运行,而且这样你会马上收到一个取消的结果。
即在onActivityResult里马上得到一个RESULT_CANCEL.
他还补充说没有很好的补救方法。可以试试用监听广播的方法。
另一个stackoverflow的问题中,有人直接回答了不能再singleInstance或singleTop模式下使用startActivityForResult()方法,不仅被采纳了,票数还不低。
剩下的一个stackoverflow问题中,有人说把singleTask改成singleTop就会正常,得到高达59票并被采纳。实际上我用的就是singTop,可是onActivityResult还是无缘无故被调用了。
startActivityForResult的文档:
public void startActivityForResult (Intent intent, int requestCode, Bundle options)
Added in API level 16
Launch an activity for which you would like a result when it finished. When this activity exits, your onActivityResult() method will be called with the given requestCode. Using a negative requestCode is the same as calling startActivity(Intent) (the activity is not launched as a sub-activity).
加载一个Activity,当它结束时你会得到结果。当这个Activty退出了,你的onActivityResult()方法会根据给出的requestCode被调用。使用一个负的requestCode和调用startActivity(intent)一样(activity不被加载成子activity)
Note that this method should only be used with Intent protocols that are defined to return a result. In other protocols (such as ACTION_MAIN or ACTION_VIEW), you may not get the result when you expect. For example, if the activity you are launching uses the singleTask launch mode, it will not run in your task and thus you will immediately receive a cancel result.
注意这个方法只能用于被定义要返回结果的Intent协议。在其他协议中(譬如ACTION_MAIN或ACTION_VIEW),你可能在你想得到结果时得不到。比如,当你正载入的Activity使用的singleTask加载模式,它不会在你的栈中运行,这样你会立马得到一个取消的结果。
As a special case, if you call startActivityForResult() with a requestCode >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your activity, then your window will not be displayed until a result is returned back from the started activity. This is to avoid visible flickering when redirecting to another activity.
有一个特例是,当你在初始的onCreate()方法或onResume()方法中用一个大于等于0的请求码调用startActivityForResult(),你的窗口在被启动的Activity返回结果前不会显示。这是为了避免跳转到另一Activity时可见的闪烁。
This method throws ActivityNotFoundException if there was no Activity found to run the given Intent.
如果运行所给Intent的Activity没被找到,该方法会抛出ActivityNotFoundException异常。
【转自】http://freetymekiyan.1kapp.com/android-activity%E7%9A%84%E5%8A%A0%E8%BD%BD%E6%A8%A1%E5%BC%8F%E5%92%8Conactivityresult%E6%96%B9%E6%B3%95%E4%B9%8B%E9%97%B4%E7%9A%84%E5%86%B2%E7%AA%81/
[转]Android Activity的加载模式和onActivityResult方法之间的冲突的更多相关文章
- Android Activity的加载模式和onActivityResult方法之间的冲突
前言 今天在调试程序时,发现在某一Activity上点击返回键会调用该Activity的onActivityResult()方法.我一开始用log,后来用断点跟踪调试半天,还是百思不得其解.因为之前其 ...
- 我的Android进阶之旅------>Android Activity的singleTask加载模式和onActivityResult方法之间的冲突
今天调试一个bug的时候,情景如下: 一个Activity A,需要用startActivityForResult方法开启Activity B.Activity B的launch mode被设置为si ...
- Android四种Activity的加载模式(转)
建议首先阅读下面两篇文章,这样才可以更好的理解Activity的加载模式: Android的进程,线程模型: http://www.cnblogs.com/ghj1976/archive/2011/0 ...
- Android四种Activity的加载模式
建议首先阅读下面两篇文章,这样才可以更好的理解Activity的加载模式: Android的进程,线程模型 http://www.cnblogs.com/ghj1976/archive/2011/04 ...
- Activity的加载模式及Intent.setFlags
在多Activity开发中,有可能是自己应用之间的Activity跳转,或者夹带其他应用的可复用Activity.可能会希望跳转到原来某个Activity实例,而不是产生大量重复的Activity. ...
- ANDROID基础ACTIVITY篇之Activity的加载模式
在这之前首先让我们先了解一下什么是Task Task,简单的说,就是一组以栈的模式聚集在一起的Activity组件集合.它们有潜在的前后驱关联,新加入的Activity组件,位于栈顶,并仅有在栈顶的A ...
- Android Activity的加载的模式
---恢复内容开始--- 本文来自http://www.cnblogs.com/lwbqqyumidi/p/3771542.html launchMode在多个Activity跳转的过程中扮演着重要的 ...
- 在主Android Activity中加载Fragment的一般简易方法 ,来模拟一个微信界面。
在Fragment的生命周期中,需要重点关注onCreate.onCreateView.onViewCreated.Activity与Fragment生命周期在设计模式上大体一致. package c ...
- Android使用glide加载.9图片的方法
我们在开发过程中会经常使用.9图片, 因为它可以使图片拉伸的时候,保证其不会失真. 而我们把.9图片放在服务器端,通过glide直接加载,会报错. 我们的解决方法是 通过sdk的aapt工具 把.9图 ...
随机推荐
- 9 云计算系列之Cinder的安装与NFS作为cinder后端存储
preface 在前面我们知道了如何搭建Openstack的keystone,glance,nova,neutron,horizon这几个服务,然而在这几个服务中唯独缺少存储服务,那么下面我们就学习块 ...
- CSS 文本缩进,行间距
文本缩进:text-indent:2.0em; 行间距:line-height:1.5em;
- windows 下为Python安装redis
最近在看<redis实战>,里面的redis示例都是Python写的,先将环境整好 启动redis redis-server.exe redis.conf 安装了python2.7 安装 ...
- …gen already exists but is not a source folder. Convert to a source folder or rename it [closed]
Right click on the project and go to "Properties" Select "Java Build Path" on th ...
- IT部门的“2/8”现状
专家的研究和大量企业实践表明,在IT项目的生命周期中,大约80%的时间与IT项目运 营维护有关,而该阶段的投资仅占整个IT投资的20%,形成了典型的“技术高消费”.“轻服务.重技术”现象.Gartne ...
- mysql中使用show variables同时查询多个参数值?show variables的使用?
需求描述: 今天在查mysq关于连接数的问题,想要通过一个show variables命令同时查出来多个值.在此记录下. 操作过程: 1.通过show variables语句的like可以匹配多个值或 ...
- 8 -- 深入使用Spring -- 3...1 Resource实现类
8.3.1 Resource实现类 Resource接口是Spring资源访问的接口,具体的资源访问由该接口的实现类完成. Spring提供的Resource接口的实现类: ⊙ UrlResource ...
- CentOS7上Mongodb安装、卸载
参考官网https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/ 一.安装 1.配置yum管理包 1).在路径/etc/ ...
- Java 数据库访问层
最近项目中需要对mysql进行操作,数据库的知识早在本科毕业那会就忘光了,这几年开发都没接触到数据库的操作. 借这个机会重新学习,数据库访问层是对数据库操作的一个封装,屏蔽底层的数据操作细节,通过使用 ...
- Nginx(九)-- Nginx实际使用配置
1.由于在nginx中需要配置很多东西,就会使得nginx.conf配置文件过于臃肿,所以我们会将配置文件合理的切分.大体的配置依然在nginx.conf中,其他的配置会放在etc下面的目录中. 2. ...