Activity工作流程原理

Step 1. Launcher.startActivitySafely
Step 2. Activity.startActivity
Step 3. Activity.startActivityForResult(intent, -1);  
Step 4. Instrumentation.execStartActivity -- ActivityManagerNative.getDefault().startActivity
Step 5. ActivityManagerProxy.startActivity
 通过Binder驱动程序就进入到ActivityManagerService的startActivity函数
Step 6. ActivityManagerService.startActivity
Step 7. ActivityStackSupervisor.startActivityMayWait
 对参数intent的内容进行解析,得到MainActivity的相关信息,保存在aInfo变量中,ResolveInfo类型
Step 8. ActivityStackSupervisor.startActivityLocked
Step 9. ActivityStackSupervisor.startActivityUncheckedLocked
 这个intent的标志值的位Intent.FLAG_ACTIVITY_NEW_TASK被置位,查看一下,当前有没有Task可以用来执行  这个Activity,需要创建一个新的Task来启动这个Activity。
Step 10. ActivityStack.resumeTopActivitiesLocked--ActivityStack.resumeTopActivityInnerLocked
Step 11. ActivityStack.startPausingLocked
mResumedActivity就是Launcher,
Step 12. ApplicationThreadProxy.schedulePauseActivity来通知Launcher进入Paused状态。
Step 13. ApplicationThread.schedulePauseActivity
Step 14. ActivityThread.sendMessage
Step 15. H.handleMessage
Step 16. ActivityThread.handlePauseActivity
 1. 如果userLeaving为true,则通过调用performUserLeavingActivity函数来调用Activity.onUserLeaveHint通知Activity,用户要离开它了;
 2. 调用performPauseActivity函数来调用Activity.onPause函数,我们知道,在Activity的生命周期中,当它要让位于其它的Activity时,系统就会调用它的onPause函数;
 3. 它通知ActivityManagerService,这个Activity已经进入Paused状态了,ActivityManagerService现在可以完成未竟的事情,即启动MainActivity了。
Step 17. ActivityManagerProxy.activityPaused
Step 18. ActivityManagerService.activityPaused
Step 19. ActivityStack.activityPaused
Step 20. ActivityStack.completePauseLocked
Step 21. ActivityStack.resumeTopActivityLokced
Step 22. ActivityStackSupervisor.startSpecificActivityLocked
Step 23. ActivityManagerService.startProcessLocked
Step 24. ActivityThread.main 在这里进行一些初始化操作,Looper.prepareMainLooper,绑定ApplicationThread
Step 25. ActivityManagerProxy.attachApplication
Step 26. ActivityManagerService.attachApplication
Step 27. ActivityManagerService.attachApplicationLocked
Step 28. ActivityStack.realStartActivityLocked
Step 29. ApplicationThreadProxy.scheduleLaunchActivity
Step 30. ApplicationThread.scheduleLaunchActivity
Step 31. ActivityThread.sendMessage
Step 32. H.handleMessage
Step 33. ActivityThread.handleLaunchActivity
Step 34. ActivityThread.performLaunchActivity
 mInstrumentation.newActivity
 makeApplication
 ContextImpl appContext = new ContextImpl();  
 appContext.init(r.packageInfo, r.token, this);  
   appContext.setOuterContext(activity);  
   CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());  
   Configuration config = new Configuration(mConfiguration); 
 activity.attach关联ContextImpl,完成Window的创建并建立自己和Window的关联
 mInstrumentation.callActivityOnCreate(activity, r.state);  
Step 35. MainActivity.onCreate
以上是Activity启动流程,参考自http://blog.csdn.net/luoshengyang/article/details/6689748
至此,Activity启动起来了,但是还不能正常工作。

ActivityThread.handleLaunchActivity {ActivityThread.performLaunchActivity —> activity.attach } — >ActivityThread.handleResumeActivity —> performResumeActivity —> activity.performResume() —> Instrumentation.callActivityOnResume 

—> activityonResume —> activity.makeVisible()

makeVisible之后,Activity才真正可见并且可交互。

(PS:低版本的系统中,可能会在handleResumeActivity中完成可见和可交互的)

void makeVisible() {
        if (!mWindowAdded) {
            ViewManager wm = getWindowManager();
            wm.addView(mDecor, getWindow().getAttributes());
            mWindowAdded = true;
        }
        mDecor.setVisibility(View.VISIBLE);
    }
这里wm.addView其实是Window的添加过程,DecorView正式被WindowManager添加到Window,这里要正确理解Widow的概念,Widow更多表示的是一种抽象的功能集合。

WindowManager.addView --> WindowManagerGlobal.addView[检查参数view,display,params --> 创建ViewRootImpl并将View添加到列表中mViews,mRoots,mParams,mDyingViews] --> ViewRootImpl.setView -- ViewRootImpl.requestLayout[mTraversalRunnable.run] --> WindowSession.addToDisplay --> Session.addToDisplay --> WMS.addWindow

至此,Activity的Window真正被添加,可以通过WMS实现交互,mDecor.setVisibility(View.VISIBLE)界面View完全可见.


你可能感兴趣的:(Android技术点)