码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2020-05-25 19:15:49      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:code   nsa   chap   let   instr   att   函数   on()   mst   

1.点击launcher图标
LauncherApp.onClick-> onClickAppShortcut()->startAppShortcutOrInfoActivity()->launcher.startActivitySafely()
    Activity.startActivity->startActivityForResult

2.页面启动逻辑
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;为空都步骤3
                    

3.第一次启动,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启动流程简单记录方便搜索以及回顾

标签:code   nsa   chap   let   instr   att   函数   on()   mst   

原文地址:https://www.cnblogs.com/yongfengnice/p/12958309.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!