Acitivy创建Context的过程(一)

page1
从本篇文章开始,我们分析一下Activity创建Context的过程.
Context是在ActivityThread的performLaunchActivity函数中创建的, 因此我们就从performLaunchActivity函数作为入口开始分析:
1 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
2 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
3
4 ActivityInfo aInfo = r.activityInfo;
5 if (r.packageInfo == null) {
6 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
7 Context.CONTEXT_INCLUDE_CODE);
8 }
9
10 ComponentName component = r.intent.getComponent();
11 if (component == null) {
12 component = r.intent.resolveActivity(
13 mInitialApplication.getPackageManager());
14 r.intent.setComponent(component);
15 }
16
17 if (r.activityInfo.targetActivity != null) {
18 component = new ComponentName(r.activityInfo.packageName,
19 r.activityInfo.targetActivity);
20 }
21
22 Activity activity = null;
23 try {
24 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
25 activity = mInstrumentation.newActivity(
26 cl, component.getClassName(), r.intent);
27 StrictMode.incrementExpectedActivityCount(activity.getClass());
28 r.intent.setExtrasClassLoader(cl);
29 if (r.state != null) {
30 r.state.setClassLoader(cl);
31 }
32 } catch (Exception e) {
33 if (!mInstrumentation.onException(activity, e)) {
34 throw new RuntimeException(
35 "Unable to instantiate activity " + component
36 + ": " + e.toString(), e);
37 }
38 }
39
40 try {
41 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
42
43 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
44 if (localLOGV) Slog.v(
45 TAG, r + ": app=" + app
46 + ", appName=" + app.getPackageName()
47 + ", pkg=" + r.packageInfo.getPackageName()
48 + ", comp=" + r.intent.getComponent().toShortString()
49 + ", dir=" + r.packageInfo.getAppDir());
50
51 if (activity != null) {
52 Context appContext = createBaseContextForActivity(r, activity);
53 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
54 Configuration config = new Configuration(mCompatConfiguration);
55 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
56 + r.activityInfo.name + " with config " + config);
57 activity.attach(appContext, this, getInstrumentation(), r.token,
58 r.ident, app, r.intent, r.activityInfo, title, r.parent,
59 r.embeddedID, r.lastNonConfigurationInstances, config);
60
61 if (customIntent != null) {
62 activity.mIntent = customIntent;
63 }
64 r.lastNonConfigurationInstances = null;
65 activity.mStartedActivity = false;
66 int theme = r.activityInfo.getThemeResource();
67 if (theme != 0) {
68 activity.setTheme(theme);
69 }
70
71 activity.mCalled = false;
72 mInstrumentation.callActivityOnCreate(activity, r.state);
73 if (!activity.mCalled) {
74 throw new SuperNotCalledException(
75 "Activity " + r.intent.getComponent().toShortString() +
76 " did not call through to super.onCreate()");
77 }
78 r.activity = activity;
79 r.stopped = true;
80 if (!r.activity.mFinished) {
81 activity.performStart();
82 r.stopped = false;
83 }
84 if (!r.activity.mFinished) {
85 if (r.state != null) {
86 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
87 }
88 }
89 if (!r.activity.mFinished) {
90 activity.mCalled = false;
91 mInstrumentation.callActivityOnPostCreate(activity, r.state);
92 if (!activity.mCalled) {
93 throw new SuperNotCalledException(
94 "Activity " + r.intent.getComponent().toShortString() +
95 " did not call through to super.onPostCreate()");
96 }
97 }
98 }
99 r.paused = true;
100
101 mActivities.put(r.token, r);
102
103 } catch (SuperNotCalledException e) {
104 throw e;
105
106 } catch (Exception e) {
107 if (!mInstrumentation.onException(activity, e)) {
108 throw new RuntimeException(
109 "Unable to start activity " + component
110 + ": " + e.toString(), e);
111 }
112 }
113
114 return activity;
115 }
第52行(ActivityThread->performLaunchActivity)调用createBaseContextForActivity函数为刚刚创建的activity对象创建Context对象, 关于performLaunchActivity函数的详细分析可以参考page2文件.
第57-59行(ActivityThread->performLaunchActivity)调用Activity的attach函数, 关于attach函数的详细分析可以参考page5文件.
page2
ActivityThread的createBaseContextForActivity函数定义如下:
1 private Context createBaseContextForActivity(ActivityClientRecord r,
2 final Activity activity) {
3 ContextImpl appContext = new ContextImpl();
4 appContext.init(r.packageInfo, r.token, this);
5 appContext.setOuterContext(activity);
6
7 // For debugging purposes, if the activity's package name contains the value of
8 // the "debug.use-second-display" system property as a substring, then show
9 // its content on a secondary display if there is one.
10 Context baseContext = appContext;
11 String pkgName = SystemProperties.get("debug.second-display.pkg");
12 if (pkgName != null && !pkgName.isEmpty()
13 && r.packageInfo.mPackageName.contains(pkgName)) {
14 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
15 for (int displayId : dm.getDisplayIds()) {
16 if (displayId != Display.DEFAULT_DISPLAY) {
17 Display display = dm.getRealDisplay(displayId);
18 baseContext = appContext.createDisplayContext(display);
19 break;
20 }
21 }
22 }
23 return baseContext;
24 }
第3行(ActivityThread->createBaseContextForActivity)会new一个ContextImpl对象. ContextImpl的构造函数的详细分析可以参考page3文件.
第4行(ActivityThread->createBaseContextForActivity)会调用ContextImpl的init函数, 关于init函数的详细分析可以参考page4文件.
第5行(ActivityThread->createBaseContextForActivity)调用ContextImpl的setOuterContext函数, ContextImpl的setOuterContext函数定义如下:
final void setOuterContext(Context context) {
mOuterContext = context;
}
这样, ContextImpl也会拿着activity对象.

第10-22行(ActivityThread->createBaseContextForActivity)是干什么呢?不知道.
page3
在这里我们分析一下ContextImpl类的构造过程.我们先来看一下ContextImpl类的继承体系, ContextImpl类的定义如下:
class ContextImpl extends Context {
public abstract class Context {

ContextImpl类的构造函数如下所示
ContextImpl() {
mOuterContext = this;
}
在ContextImpl的构造函数中, 只是初始化了成员变量mOuterContext, 使之指向该ContextImpl对象.
成员变量mOuterContext的定义如下:
private Context mOuterContext;

妈的, 就这么简单.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值