jiaming1992
V2EX  ›  Android

求教 Android Activity 小知识

  •  
  •   jiaming1992 · Sep 19, 2017 · 14224 views
    This topic created in 3338 days ago, the information mentioned may be changed or developed.

    如果在一个 singleTop 或者 singleInstance 的 ActivityA 中通过 startActivityForResult()方法来启动另一个 ActivityB,那么系统将直接返回 Activity.Result_CANCELED 而不会再去等待返回,这是由于系统在 Framework 层做了对这两种启动模式的限制,因为 Android 开发者认为,不同 Task 之间默认是不能传递数据的,如果一定要传递,那则只能通过 Intent 来绑定数据 ----《 Android 群英传》

    新人表示有点困惑,这句话在自己写的 Demo 中测试怎么没有效果呢?
    13 replies  •  2017-10-20 09:25:25 +08:00
    yuPD97Yeed4QM245
    yuPD97Yeed4QM245
    Sep 19, 2017 via Android
    1
    Android 5.0 以上和 Android 5.0 以下效果不同
    jiaming1992
    jiaming1992
    OP
    Sep 19, 2017
    2
    @wl9739 我把 sdk 换成 19 还是一样效果呢 0_0
    jiaming1992
    jiaming1992
    OP
    Sep 19, 2017
    3
    @wl9739 搞明白了,谢谢
    shenwu35
    shenwu35
    Sep 19, 2017❤️3
    4
    @jiaming1992 跟其他论坛一样的风格。搞明白后不说原因的。然后其他人就开始问,怎么搞明白的。然后就是死活不说的玩消失。
    allenlee7c9
    allenlee7c9
    Sep 19, 2017 via Android
    5
    @shenwu35 楼主还在消失中
    yufz
    yufz
    Sep 19, 2017
    6
    楼主写错代码了而已,他的两个 Activity 还在同一个 Task 里面,所以没有效果
    zhoucan007
    zhoucan007
    Sep 19, 2017
    7
    注意一下,singleTop 时,两个 Activity 还是在同一个 Task 里面的。
    xzg1993
    xzg1993
    Sep 19, 2017
    8
    @jiaming1992 你这样以后就不要来问东西了,说完了之后就消失了?
    jiaming1992
    jiaming1992
    OP
    Sep 19, 2017
    9
    @xzg1993 哈哈 这是论坛吧 也不是 qq 群 不能隔个五分钟刷新一次吧
    jiaming1992
    jiaming1992
    OP
    Sep 19, 2017
    10
    我是把 tagetsdk 换成 19,但是,跑在了 25 的模拟器上 所以没有效果。。 但是换成 19 的模拟器 就出现上面说的情况了,话说 Android API 文档里也没说这个。。
    jiaming1992
    jiaming1992
    OP
    Sep 19, 2017
    11
    @shenwu35 受教了,以后肯定搞明白了再把解决方案说一遍。
    Poarry
    Poarry
    Oct 19, 2017
    12
    <p>Note that this method should only be used with Intent protocols
    * that are defined to return a result. In other protocols (such as
    * {@link Intent#ACTION_MAIN} or {@link Intent#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.
    *

    ------来自源码
    jiaming1992
    jiaming1992
    OP
    Oct 20, 2017
    13
    @Poarry 亲测实际只对 singleTask,singleInstance 有影响,所以书中 singleTop 写的可能有误,而且源码也没说 5.0 以上 5.0 以下实现不同 0.0
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   987 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 19:19 · PVG 03:19 · LAX 11:19 · JFK 14:19
    ♥ Do have faith in what you're doing.