我的问题属于第一种情况,更改后下确有回传结果。

(http://www.cnblogs.com/tt_mc/p/3586834.html)

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异常。


Activity的加载模式

Use Cases Launch Mode Multiple Instances? Comments
Normal launches for most activities "standard" Yes Default. The system always creates a new instance of the activity in the target task and routes the intent to it.
"singleTop" Conditionally If an instance of the activity already exists at the top of the target task, the system routes the intent to that instance through a call to itsonNewIntent() method, rather than creating a new instance of the activity.
Specialized launches
(not recommended for general use)
"singleTask" No The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to itsonNewIntent() method, rather than creating a new one.
"singleInstance" No Same as "singleTask", except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task.

singleTop模式,可用来解决栈顶多个重复相同的Activity的问题。

singleTask模式和后面的singleInstance模式都是只创建一个实例的。

当intent到来,需要创建singleTask模式Activity的时候,系统会检查栈里面是否已经有该Activity的实例。如果有直接将intent发送给它。

singleInstance模式解决了这个问题(绕了这么半天才说到正题)。让这个模式下的Activity单独在一个task栈中。这个栈只有一个Activity。导游应用和google地图应用发送的intent都由这个Activity接收和展示。

总结

后来我改变了onActivityResult里面ResultCode为RESULT_OK时刷新界面的具体实现方法,可是onActivityResult还是会自己被调用,只是暂时没触发任何bug,可它还是个定时炸弹啊。

以后在选择Activity的加载模式时,要考虑onActivtyResult方法与之存在冲突。

android onActivityResult无效或先执行或无回传问题的更多相关文章

  1. android onActivityResult的执行

    1.如果activity中重写了onActivityResult函数,同时添加在该activity的fragment也重写了onActivtyResult函数,那么会执行Activity的onActi ...

  2. EasyTouch绑定事件在电脑上点击有效Android上无效的解决方法

    最近做一个RPG类的游戏发现使用EasyTouch虚拟摇杆插件在电脑上点击有效Android上无效,查找资料发现是Easy Joystick中的一个属性interaction type要设置成 Dir ...

  3. Android之拨号界面图片风格,无信息默认显示界面修改

    Android之拨号界面图片风格,无信息默认显示界面修改 点开Dialer app,出现拨号,联系人,收藏三个选项卡,也就是三个Fragment,在三个界面都没有信息的时候会显示一个时钟,联系人,收藏 ...

  4. SublimeText3按ctrl+b执行python无反应

    现象:在Sublime中打开.py文件,按”ctrl+b”执行时无反应.点击工具->编译系统中已经有且识别到Python,但执行”run(ctrl+shift+b)”时无反应,Sublime左下 ...

  5. click 在网页测试手机模式下无效,不能执行。调成非手机模式即可

    click  在网页测试手机模式下无效,不能执行. 调成非手机模式即可

  6. Android onActivityResult()运行时刻的问题

    今天在开发过程中遇到一个很是怪异的问题,就是方法onActivityResult的执行问题,问题是当我从当前的Activity跳转的时候,尚未做任何动作,onActivityResult()就已经执行 ...

  7. 使用sqluldr2进行oracle数据库抽取时执行后无反应,也无日志

    使用sqluldr2进行oracle数据库表数据抽取时遇到执行后无反应,也不报错,也无日志输出的情况. 经过排查之后发现时由于oracle账户密码快要过期导致的(这也能出问题,我服,类似的plsql连 ...

  8. android lsitview setOnItemLongClickListener 无效或不执行

    今天遇到了lsitview的setOnItemLongClickListener的方法不执行,我是在listview中的每一个ITEM都存放了不同的布局:给整个item布局设置了点击事件onClick ...

  9. [原] Android performClick无效,UI线程理解

    原因 开发过程中遇到button.performClick()无效,原因是View.performClick()需要再UI线程中调用才会有效执行. 响应系统调用的方法(比如报告用户动作的onKeyDo ...

随机推荐

  1. HTML5+CSS3实现图片可倾斜摆放的动画相册效果

    先看看效果:其中鼠标悬浮在图片上会有动态效果图 直接上代码: css文件 @CHARSET "UTF-8"; *{ padding:0px; margin:0px; } div{ ...

  2. 一个简单的Webservice的demo,简单模拟服务

    前段时间一直在学习WCF,匆匆忙忙的把<WCF全面解析>和<WCF服务编程>看了一遍,好多东西都不是很懂,又听了一下WCF分布式开发的网络教程,算是马马虎虎的明白点了.回顾了一 ...

  3. authorization与URL授权

    利用Web.config中的authorization标签设置授权属于URL授权. 使用 URL 授权 通过 URL 授权,您可以显式允许或拒绝某个用户名或角色对特定目录的访问权限.为此,请在该目录的 ...

  4. Xamarin.ios引用第三方SDK

    引言 诚然,Xamarin是个优秀的跨平台解决方案,但毕竟还是不能将Native中所有的方法都直接实现.诸如各种第三方库,也都只有java/oc原生版本的SDK,无法直接拿过来直接使用.但,不能直接拿 ...

  5. centos安装禅道的步骤

    1.下载 XAMPP 套件: https://sourceforge.net/projects/xampp/files/XAMPP%20Linux/stats/timeline  下载的文件是 xam ...

  6. WinFrom窗体始终置顶

    调用WindowsAPI使窗体始终保持置顶效果,不被其他窗体遮盖: [DllImport("user32.dll", CharSet = CharSet.Auto)] privat ...

  7. 微信公众平台自动回复wechatlib.jar的生成及wechatlib解析

    微信公众平台出来有一段时日了,官方提供的自动回复的接口调用大致是这么些类型(text/image/location/link),每个项目都如此拷贝代码,在笔者看来比较麻烦,今天乘着点闲暇的时间特意将这 ...

  8. Lind.DDD.Authorization用户授权介绍

    回到目录 Lind.DDD.Authorization是Lind.DDD框架的组成部分,之所以把它封装到框架里,原因就是它的通用性,几乎在任何一个系统中,都少不了用户授权功能,用户授权对于任何一个系统 ...

  9. GJM:Unity导入百度地图SDK [转载]

    感谢您的阅读.喜欢的.有用的就请大哥大嫂们高抬贵手"推荐一下"吧!你的精神支持是博主强大的写作动力以及转载收藏动力.欢迎转载! 版权声明:本文原创发表于 [请点击连接前往] ,未经 ...

  10. GJM : 各大开发游戏引擎

    感谢您的阅读.喜欢的.有用的就请大哥大嫂们高抬贵手"推荐一下"吧!你的精神支持是博主强大的写作动力以及转载收藏动力.欢迎转载! 版权声明:本文原创发表于 [请点击连接前往] ,未经 ...