Activity启动过程分析

    技术2026-03-29  9

    启动activity: **android.process.acore进程 1.Acticity.startActivityForResult() 2.Instrumention.execStartActivity(); 3.ActivityManagerNative.getDefault()                 .startActivity()   3.1 ActivityManagerNative.getDefault()返回的是ActivityManagerProxy的实例,   它只是一个代理类,这个代理类实际上代理的是IBinder b = ServiceManager.getService("activity");   这个Service。   3.2 这个Service是什么时候添加进来的呢?   在SystemServer.java的run()中有调用   ActivityManagerService.setSystemProcess();       ServiceManager.addService("activity", m);       这里还会添加许多系统关键服务。       (TODO:查看在SystemServer的Log输出)       **system_process进程 4 ActivityManagerNative     |--ActivityManagerService         在ActivityManagerNative的onTransact方法中最终提供了服务:     case START_ACTIVITY_TRANSACTION:       ActivityManagerService.startActivity();         --startActivityLocked(IApplicationThread caller,             Intent intent, String resolvedType,             Uri[] grantedUriPermissions,             int grantedMode, ActivityInfo aInfo, IBinder resultTo,             String resultWho, int requestCode,             int callingPid, int callingUid, boolean onlyIfNeeded,             boolean componentSpecified)           --startActivityUncheckedLocked(r, sourceRecord,                 grantedUriPermissions, grantedMode, onlyIfNeeded, true)           在这个方法里面检查权限,解析intent中的Flag。。。           --startActivityLocked(HistoryRecord r, boolean newTask)             --resumeTopActivityLocked(HistoryRecord prev)               --startSpecificActivityLocked(HistoryRecord r,boolean andResume, boolean checkConfig)                 --startProcessLocked(String processName,ApplicationInfo info, boolean knownToBeDead, int intentFlags,String hostingType, ComponentName hostingName)                   --startProcessLocked(ProcessRecord app,String hostingType, String hostingNameStr)                     在这里启动一个进程用来host这个应用                     int pid = Process.start("android.app.ActivityThread",                     mSimpleProcessManagement ? app.processName : null, uid, uid,                     gids, debugFlags, null);                     ActivityManagerService.java --startSpecificActivityLocked(HistoryRecord r,boolean andResume, boolean checkConfig)   --realStartActivityLocked(HistoryRecord r,ProcessRecord app, boolean andResume, boolean checkConfig)     --app.thread.scheduleLaunchActivity                                   //scheduleLaunchActivity()@IApplicationThread.java       --scheduleLaunchActivity()@ActivityThread.java                      //这里实际是ApplicationThreadNative提供的服务         --handleMessage()@H$ActivityThread.java           --handleLaunchActivity()@ActivityThread.java             --Activity performLaunchActivity(ActivityRecord r, Intent customIntent)@ActivityThread.java  //这时真正的Activity对象被构造出来               --mInstrumentation.newActivity()                            //通过反射构造出Activity对象               --activity.attach()                                         //初始化Activity,生成一个window对象,设置各种状态等等               --mInstrumentation.callActivityOnCreate(activity, r.state);  //调用Activity的onCreate()方法                           到这里,我们自己写的activity的onCreate()方法已经被系统调用了,接下来依次回调生命周期方法:               --activity.performStart();                 --mInstrumentation.callActivityOnStart(this);               --mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);               --mInstrumentation.callActivityOnPostCreate(activity, r.state);               --mActivities.put(r.token, r);                               //将这个activity入栈                           然后就要调用onResume()方法了:             --handleResumeActivity(IBinder token, boolean clearHide, boolean isForward)               --performResumeActivity(token, clearHide);                 --r.activity.performResume();                   --performRestart()@Activity.java;                     --mInstrumentation.callActivityOnRestart(this);                     --mInstrumentation.callActivityOnStart(this);                 --mInstrumentation.callActivityOnResume(this);                 onResume()已经调用完毕,一个activity的逻辑处理结束了,但是这时候屏幕上还不会显示任何东西,因为View还没有添加进去                                               --r.window.getDecorView();                                   //开始把DecorView添加进Window               --wm.addView(decor, l);                         至此一个Activity启动结束。
    最新回复(0)