Acitivy创造Context的过程(一)
发布时间:2010-05-29 22:17:06 文章来源:www.iduyao.cn 采编人员:星星草
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;
妈的, 就这么简单.
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。
其他相似内容:
-
【IOS】高仿糗事百科客户端(基本实现了除注册,评论之外的功能)
【原创作品, 欢迎转载,转载请在明显处注明! 谢谢。
原文地址:h...
-
数独游戏(1)之绘制九宫格,并初始化数据
<数独游戏>之绘制九宫格,并初始化数据
1.在工程里的res/values目录下新建一个colors....
-
关于listview的setAdapter和notifyDataSetChanged
做了个需要更新listview的项目,在listview更新时遇到问题,抓耳挠腮半天仍未能解...
-
最完整的让cocos2d游戏适配IPAD Retina的教程
暂时在网上还没找到一个让cocos2d开发的IPhone游戏快速地适应IPAD2 Retina模式的教...
-
使用ContentObserver监听ContentProvider中数据的变化
如果ContentProvider的访问者需要知道ContentProvider中的数据发生了变化,...
-
Andriod 实现可拖动列表
在做一个应用时,有一个需求,需要实现像iphone天气的城市列表界面的可拖动功能。其实android已...
-
类似APP大厅程序的实现
由于Android可以按照一定的规则,启动另外一个应用程序的Activity,并可以传递一些数据,因此下面的需求,在原则...
-
编写小程序查看CSDN博客信息(ASIHTTPRequest+TFHpple)
作为一个iOS练习,使用ASIHTTPRequest+TFHpple写了个小程序读取CSDN博客...
-
Objective-C中nil与release的区别与用法
做IOS开发的估计都对Objective-C的内存管理机制很头疼,一不小心程序就会出内存泄露...
-
联想抗衡苹果之路:用苹果的方式打败苹果?
联想教父柳传志说过,联想历史上曾经历三次生死危机,但都死里逃生,并一次次验证着“大难不...