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

Posted yongfengnice

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android开发Activity启动流程简单记录方便搜索以及回顾相关的知识,希望对你有一定的参考价值。

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启动流程简单记录方便搜索以及回顾的主要内容,如果未能解决你的问题,请参考以下文章

Android 11(platfrom 30)APP启动流程(含Activity)核心点记录

Android 10.0 Activity的启动流程

Android 11(platfrom 30)APP启动流程(含Activity)核心点记录

Android一张图看懂Activity的启动流程

Android一张图看懂Activity的启动流程

Android | Activity 启动流程分析