startActivityForResult和launchMode的兼容性

概述

当下的安卓版本其中有超过一半的是小于5.0,5.0以上的也在呈现增长着趋势。就用我参与开发的app来说吧,一般最小兼容到4.0。至于2.3,照项目经理的话来说,还在用2.3那还真是活在了五年前了。


安卓版本如此之多,谷歌也是一年一更替,多少API都是打死在沙滩上。但是我们开发又不能只兼顾着一个版本,至少也要涵盖大部分的版本。要不然,群众人民不买账啊。

话说startActivityForResult和launchMode在5.0之前和在5.0之后有着不同的使用限制。本文,就是来讨论一下其中的区别。

下图展示了具体关系:


那么,我先从launchMode启动模式来入手看看


5.0以前的源码:

扫描二维码关注公众号,回复: 2715495 查看本文章
        if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
            // For whatever reason this activity is being launched into a new
            // task...  yet the caller has requested a result back.  Well, that
            // is pretty messed up, so instead immediately send back a cancel
            // and let the new task continue launched as normal without a
            // dependency on its originator.
            Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
            r.resultTo.task.stack.sendActivityResultLocked(-1,
                    r.resultTo, r.resultWho, r.requestCode,
                Activity.RESULT_CANCELED, null);
            r.resultTo = null;
        }


5.0及5.0以后的源码:

        if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
            // For whatever reason this activity is being launched into a new
            // task...  yet the caller has requested a result back.  Well, that
            // is pretty messed up, so instead immediately send back a cancel
            // and let the new task continue launched as normal without a
            // dependency on its originator.
            Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
            r.resultTo.task.stack.sendActivityResultLocked(-1,
                    r.resultTo, r.resultWho, r.requestCode,
                    Activity.RESULT_CANCELED, null);
            r.resultTo = null;
        }

        if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
            launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
        }

猜你喜欢

转载自blog.csdn.net/shirakawakanaki/article/details/53100000