Activity的onCreate方法是怎样被调用的?
按前面源码分析的介绍,在Activity
的onCreate
方法中,添加如下的log:
android.util.Log.i("wztest", "Activity onCreate", new Exception());
重新编译后,启动联系人的一个页面,过滤log,有如下的输出:
05-24 21:06:43.034 2159 2159 I wztest : Activity onCreate
05-24 21:06:43.034 2159 2159 I wztest : java.lang.Exception
05-24 21:06:43.034 2159 2159 I wztest : at android.app.Activity.onCreate(Activity.java:990)
05-24 21:06:43.034 2159 2159 I wztest : at android.support.v4.app.SupportActivity.onCreate(SupportActivity.java:66)
05-24 21:06:43.034 2159 2159 I wztest : at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:290)
05-24 21:06:43.034 2159 2159 I wztest : at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:84)
05-24 21:06:43.034 2159 2159 I wztest : at com.android.contacts.activities.AppCompatTransactionSafeActivity.onCreate(AppCompatTransactionSafeActivity.java:33)
05-24 21:06:43.034 2159 2159 I wztest : at com.android.contacts.AppCompatContactsActivity.onCreate(AppCompatContactsActivity.java:81)
05-24 21:06:43.034 2159 2159 I wztest : at com.android.contacts.activities.PeopleActivity.onCreate(PeopleActivity.java:352)
05-24 21:06:43.034 2159 2159 I wztest : at android.app.Activity.performCreate(Activity.java:7000)
05-24 21:06:43.034 2159 2159 I wztest : at android.app.Activity.performCreate(Activity.java:6991)
05-24 21:06:43.034 2159 2159 I wztest : at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
05-24 21:06:43.034 2159 2159 I wztest : at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
05-24 21:06:43.034 2159 2159 I wztest : at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
05-24 21:06:43.034 2159 2159 I wztest : at android.app.ActivityThread.-wrap11(Unknown Source:0)
05-24 21:06:43.034 2159 2159 I wztest : at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
05-24 21:06:43.034 2159 2159 I wztest : at android.os.Handler.dispatchMessage(Handler.java:106)
05-24 21:06:43.034 2159 2159 I wztest : at android.os.Looper.loop(Looper.java:164)
05-24 21:06:43.034 2159 2159 I wztest : at android.app.ActivityThread.main(ActivityThread.java:6494)
05-24 21:06:43.034 2159 2159 I wztest : at java.lang.reflect.Method.invoke(Native Method)
05-24 21:06:43.034 2159 2159 I wztest : at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
05-24 21:06:43.034 2159 2159 I wztest : at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
ActivityThread
的handleLaunchActivity
方法是如何被调用的?
通过搜索可知,由ApplicationThread
的 scheduleLaunchActivity
方法调用
ApplicationThread
类定义如下,说明是其它进程跨进程调用过来的
private class ApplicationThread extends IApplicationThread.Stub
然后在,ApplicationThread
的 scheduleLaunchActivity
方法中添加一个log
android.util.Log.i("wztest", "ApplicationThread scheduleLaunchActivity", new Exception());
此时log输出如下:
可知是通过binder远程调用过来的,其实就是system_server
调用过来的
ApplicationThread
的 scheduleLaunchActivity
方法如何被调用?
在源码中搜索,或者通过命令行来搜索
grep "\.scheduleLaunchActivity" ./ -rn
-r 在多级目录中对文本进行递归搜索
-n 输出包含匹配字符串的行
可知和ActivityStackSupervisor
类相关
在ActivityStackSupervisor
的realStartActivityLocked
方法,添加log
android.util.Log.i("wztest", "ActivityStackSupervisor realStartActivityLocked", new Exception());
log输出如下:
05-24 22:08:05.203 1547 1678 I wztest : ActivityStackSupervisor realStartActivityLocked
05-24 22:08:05.203 1547 1678 I wztest : java.lang.Exception
05-24 22:08:05.203 1547 1678 I wztest : at com.android.server.am.ActivityStackSupervisor.realStartActivityLocked(ActivityStackSupervisor.java:1315)
05-24 22:08:05.203 1547 1678 I wztest : at com.android.server.am.ActivityStackSupervisor.startSpecificActivityLocked(ActivityStackSupervisor.java:1580)
05-24 22:08:05.203 1547 1678 I wztest : at com.android.server.am.ActivityStack.resumeTopActivityInnerLocked(ActivityStack.java:2726)
05-24 22:08:05.203 1547 1678 I wztest : at com.android.server.am.ActivityStack.resumeTopActivityUncheckedLocked(ActivityStack.java:2255)
05-24 22:08:05.203 1547 1678 I wztest : at com.android.server.am.ActivityStackSupervisor.resumeFocusedStackTopActivityLocked(ActivityStackSupervisor.java:2094)
05-24 22:08:05.203 1547 1678 I wztest : at com.android.server.am.ActivityStack.completePauseLocked(ActivityStack.java:1486)
05-24 22:08:05.203 1547 1678 I wztest : at com.android.server.am.ActivityStack.activityPausedLocked(ActivityStack.java:1413)
05-24 22:08:05.203 1547 1678 I wztest : at com.android.server.am.ActivityManagerService.activityPaused(ActivityManagerService.java:7441)
05-24 22:08:05.203 1547 1678 I wztest : at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:317)
05-24 22:08:05.203 1547 1678 I wztest : at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2919)
05-24 22:08:05.203 1547 1678 I wztest : at android.os.Binder.execTransact(Binder.java:697)
05-24 22:08:05.207 2167 2181 I wztest : ApplicationThread scheduleLaunchActivity
05-24 22:08:05.207 2167 2181 I wztest : java.lang.Exception
05-24 22:08:05.207 2167 2181 I wztest : at android.app.ActivityThread$ApplicationThread.scheduleLaunchActivity(ActivityThread.java:788)
05-24 22:08:05.207 2167 2181 I wztest : at android.app.IApplicationThread$Stub.onTransact(IApplicationThread.java:196)
05-24 22:08:05.207 2167 2181 I wztest : at android.os.Binder.execTransact(Binder.java:697)
05-24 22:08:05.268 2167 2167 I wztest : Activity onCreate
05-24 22:08:05.268 2167 2167 I wztest : java.lang.Exception
05-24 22:08:05.268 2167 2167 I wztest : at android.app.Activity.onCreate(Activity.java:990)
05-24 22:08:05.268 2167 2167 I wztest : at android.support.v4.app.SupportActivity.onCreate(SupportActivity.java:66)
05-24 22:08:05.268 2167 2167 I wztest : at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:290)
05-24 22:08:05.268 2167 2167 I wztest : at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:84)
05-24 22:08:05.268 2167 2167 I wztest : at com.android.contacts.activities.AppCompatTransactionSafeActivity.onCreate(AppCompatTransactionSafeActivity.java:33)
05-24 22:08:05.268 2167 2167 I wztest : at com.android.contacts.AppCompatContactsActivity.onCreate(AppCompatContactsActivity.java:81)
05-24 22:08:05.268 2167 2167 I wztest : at com.android.contacts.activities.PeopleActivity.onCreate(PeopleActivity.java:352)
05-24 22:08:05.268 2167 2167 I wztest : at android.app.Activity.performCreate(Activity.java:7000)
05-24 22:08:05.268 2167 2167 I wztest : at android.app.Activity.performCreate(Activity.java:6991)
05-24 22:08:05.268 2167 2167 I wztest : at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
05-24 22:08:05.268 2167 2167 I wztest : at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2732)
05-24 22:08:05.268 2167 2167 I wztest : at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2857)
05-24 22:08:05.268 2167 2167 I wztest : at android.app.ActivityThread.-wrap11(Unknown Source:0)
05-24 22:08:05.268 2167 2167 I wztest : at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1590)
05-24 22:08:05.268 2167 2167 I wztest : at android.os.Handler.dispatchMessage(Handler.java:106)
05-24 22:08:05.268 2167 2167 I wztest : at android.os.Looper.loop(Looper.java:164)
05-24 22:08:05.268 2167 2167 I wztest : at android.app.ActivityThread.main(ActivityThread.java:6495)
05-24 22:08:05.268 2167 2167 I wztest : at java.lang.reflect.Method.invoke(Native Method)
05-24 22:08:05.268 2167 2167 I wztest : at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
05-24 22:08:05.268 2167 2167 I wztest : at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
综合参考以上信息和《Android进阶解密》中相关内容,Activity
的onCreate
方法被调用的过程,可总结为如下的形式: