博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android中Activity的startActivity和Context的startActivity有什么不同
阅读量:2073 次
发布时间:2019-04-29

本文共 2824 字,大约阅读时间需要 9 分钟。

原文: http://tryenough.com/android-startActivity

在使用中的不同

1.在Activity中跳转到其他的Activity时,两种使用方法是一样的:

this.startActivity(intent);context.startActivity(intent);复制代码

2.从非 Activity (例如从其他Context中)启动Activity则必须给intent设置Flag:FLAG_ACTIVITY_NEW_TASK:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) ; mContext.startActivity(intent);复制代码

探究一下为什么会有这方面的差异


原文: http://tryenough.com/android-startActivity

首先看下Activity和context的继承关系:

 

1

 

要知道Activity和context中的StartActivity有什么区别,我们看下它们分别是怎么实现startActivity函数的:

原文: http://tryenough.com/android-startActivity

1.Context是抽象类,它的startActivity函数是抽象方法:

public abstract void startActivity(@RequiresPermission Intent intent);复制代码

2.ContextWrapper类只是调用了Context的实现:

@Overridepublic void startActivity(Intent intent) {    mBase.startActivity(intent);}复制代码

3.ContextThemeWrapper中没有实现此方法

4.Activity中:

@Override    public void startActivity(Intent intent, @Nullable Bundle options) {        if (options != null) {            startActivityForResult(intent, -1, options);        } else {            // Note we want to go through this call for compatibility with            // applications that may have overridden the method.            startActivityForResult(intent, -1);        }    }复制代码

由此可见,在Activity中无论是使用哪一种startActivity方法都会调用到Activity自身的方法,所以是一样的。

原文: http://tryenough.com/android-startActivity

然而在其他的Context子类,例如ContextImpl.java中的实现,会检查有没有设置Flag:FLAG_ACTIVITY_NEW_TASK,否则会报错:

@Override    public void startActivity(Intent intent, Bundle options) {        warnIfCallingFromSystemProcess();        // Calling start activity from outside an activity without FLAG_ACTIVITY_NEW_TASK is        // generally not allowed, except if the caller specifies the task id the activity should        // be launched in. A bug was existed between N and O-MR1 which allowed this to work. We        // maintain this for backwards compatibility.        final int targetSdkVersion = getApplicationInfo().targetSdkVersion;        if ((intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) == 0                && (targetSdkVersion < Build.VERSION_CODES.N                        || targetSdkVersion >= Build.VERSION_CODES.P)                && (options == null                        || ActivityOptions.fromBundle(options).getLaunchTaskId() == -1)) {            throw new AndroidRuntimeException(                    "Calling startActivity() from outside of an Activity "                            + " context requires the FLAG_ACTIVITY_NEW_TASK flag."                            + " Is this really what you want?");        }        mMainThread.getInstrumentation().execStartActivity(                getOuterContext(), mMainThread.getApplicationThread(), null,                (Activity) null, intent, -1, options);    }复制代码

原文: http://tryenough.com/android-startActivity

这也就是为什么Activity的startActivity和Context的startActivity会有上面那些使用上的不同的原因了。

转载地址:http://ptvmf.baihongyu.com/

你可能感兴趣的文章
HTML中表格的使用
查看>>
(模板 重要)Tarjan算法解决LCA问题(PAT 1151 LCA in a Binary Tree)
查看>>
(PAT 1154) Vertex Coloring (图的广度优先遍历)
查看>>
(PAT 1115) Counting Nodes in a BST (二叉查找树-统计指定层元素个数)
查看>>
(PAT 1143) Lowest Common Ancestor (二叉查找树的LCA)
查看>>
(PAT 1061) Dating (字符串处理)
查看>>
(PAT 1118) Birds in Forest (并查集)
查看>>
数据结构 拓扑排序
查看>>
(PAT 1040) Longest Symmetric String (DP-最长回文子串)
查看>>
(PAT 1145) Hashing - Average Search Time (哈希表冲突处理)
查看>>
(1129) Recommendation System 排序
查看>>
PAT1090 Highest Price in Supply Chain 树DFS
查看>>
(PAT 1096) Consecutive Factors (质因子分解)
查看>>
(PAT 1019) General Palindromic Number (进制转换)
查看>>
(PAT 1073) Scientific Notation (字符串模拟题)
查看>>
(PAT 1080) Graduate Admission (排序)
查看>>
Play on Words UVA - 10129 (欧拉路径)
查看>>
mininet+floodlight搭建sdn环境并创建简答topo
查看>>
【linux】nohup和&的作用
查看>>
Set、WeakSet、Map以及WeakMap结构基本知识点
查看>>