android开发Activity启动流程简单记录方便搜索以及回顾

金鞍玉勒寻芳客,未信我庐别有春。这篇文章主要讲述android开发Activity启动流程简单记录方便搜索以及回顾相关的知识,希望能为你提供帮助。

1.点击launcher图标 LauncherApp.onClick-> onClickAppShortcut()-> startAppShortcutOrInfoActivity()-> launcher.startActivitySafely() Activity.startActivity-> startActivityForResult2.页面启动逻辑 Instrumentation.exeStartActivity -> ams.getService.startActivity(ActivityTaskManager.getService().startActivity) -> startActivityAsUser() ActivityStarter.startActivity -> ActivityStarter.startActivityUnchecked() -> mRootActivityContainer.resumeFocusedStacksTopActivities(); ActivityStack.resumeTopActivityUncheckedLocked() -> ActivityStack.resumeTopActivityInnerLocked() mStackSupervisor.startSpecificActivityLocked() -> 判断进程是否已经存在,即ApplicationThread是否为空 //逻辑判断if(ApplicationThread!=null) mStackSupervisor.realStartActivityLocked() else mService.startProcessAsync()开启新进程 //如果ApplicationThread不为空,走步骤4;为空都步骤33.第一次启动,ApplicationThread为空进程不存在,调用Process.start开启新进程-> 入口是android.app.ActivityThread ActivityManagerService.startProcess() -> mProcessList.startProcessLocked()-> Process.start()开启新进程,入口是android.app.ActivityThread ActivityThread进程启动main函数执行,逻辑进入该main函数 ActivityThread.attach()-> IActivityManager.attachApplication(mAppThread, startSeq)即AMS.attachApplication(mAppThread, startSeq); //mAppThread即ApplicationThread是ActivityThread的一个内部类,IActivityManager实现类是ActivityManagerService //也就是将ApplicationThread实例attach给ActivityManagerService(AMS),由AMS管理Application生命周期函数调用 ActivityManagerService.attachApplicationLocked()-> ActivityThread.bindApplication()-> ActivityThread.handleBindApplication data.info.makeApplication()即LoadApk.makeApplicationc创建Application实例app-> mInstrumentation.callApplicationOnCreate(app)-> app.onCreate() 自此,进程被创建,application的onCreate方法被回调。4.如果进程已经存在了 mStackSupervisor.realStartActivityLocked()-> clientTransaction.addCallback(LaunchActivityItem.obtain())、scheduleTransaction(clientTransaction) ActivityThread.scheduleTransaction()-> LaunchActivityItem.execute()-> client.handleLaunchActivity()、client实例就是ActivityThread ActivityThread.handleLaunchActivity()-> ActivityThread.performLaunchActivity() mInstrumentation.newActivity()-> mInstrumentation.callActivityOnCreate()-> Activity.performCreate()-Activity.onCreate()//This manages the execution of the main thread in an application process 管理主线程的执行操作 //scheduling and executing activities, broadcasts, and other operations on it as the activity manager requests//调度执行AM请求的操作//ApplicationThread时ActivityThread的内部类,ApplicationThread被attach给IActivityMgr持有(IActivityMgr的实现类是AMS) //MakeApplication,入口是android.app.Application

【android开发Activity启动流程简单记录方便搜索以及回顾】 

    推荐阅读