Android8.0 Activity 启动流程
程序员文章站
2024-02-29 19:32:58
...
参考别人的分享,总结了一下代码的调用流程,做了一下笔记
startActivity() -> startActivityForResult() -> mInstrumentation.execStartActivity() ->
ActivityManager.getService().startActivity(),会通过Binder机制,调用到ActivityManagerService
的startActivity()方法。
ActivityManagerService{
startActivity() -> startActivityAsUser() -> ActivityStarter.startActivityMayWait()
}
ActivityStarter{
startActivityMayWait() -> startActivityLocked() -> startActivity() ->
经辗转调用ActivityStackSupervisor.realStartActivityLocked() ->
IApplicationThread.scheduleLaunchActivity() 通过Binder机制,调用到
ApplicationThread.schedulelaunchActivity()
}
看一下 ActivityThread.ApplicationThread.schedulelaunchActivity()
schedulelaunchActivity() -> 通过Handler机制,发送消息sendMessage(H.LAUNCH_ACTIVITY, r)
-> handleLaunchActivity()
-> performLaunchActivity(){
mInstrumentation.newActivity() 使用类加载器,类名通过反射,创建Activity对象
makeApplication(){
Instrumentation.newApplication() 使用类加载器,类名通过反射,创建Application对象
callApplicationOnCreate() -> app.onCreate()
}
activity.attach() 创建了窗口对象PhoneWindow
Instrumentation.callActivityOnCreate() -> activity.performCreate() ->
调用Activity的onCreate()
}
调用Activity的onCreate() 一个Activity 也就启动了。