acitivity启动原理

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了acitivity启动原理相关的知识,希望对你有一定的参考价值。

Activity管理的核心是AcitivityManagerService,是一个独立的进程;

ActiveThread是每一个应用程序所在进程的主线程,循环的消息处理;

ActiveThread与AcitivityManagerService的通信是属于进程间通信,使用binder机制;

 

 

1 使用代理模式启动到ActivityManagerService中执行;

2 创建ActivityRecord到mHistory记录中;

3 通过socket通信到Zgote相关类创建process;

4 通过ApplicatonThread与ActivityManagerService建立通信;

5 ActivityManagerService通知ActiveThread启动Activity的创建;

6 ActivityThread创建Activity加入到mActivities中并开始调度Activity执行;

 

Activity:这个大家都熟悉,startActivity方法的真正实现在Activity中Instrumentation:用来辅助Activity完成启动Activity的过程ActivityThread(包含ApplicationThread + ApplicationThreadNative + IApplicationThread):真正启动Activity的实现都在这里

 

public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
//一般的Activity其mParent为null,mParent常用在ActivityGroup中,ActivityGroup已废弃
if (mParent == null) {
//这里会启动新的Activity,核心功能都在mMainThread.getApplicationThread()中完成
Instrumentation.ActivityResult ar =
mInstrumentation.execStartActivity(
this, mMainThread.getApplicationThread(), mToken, this,
intent, requestCode, options);
if (ar != null) {
//发送结果,即onActivityResult会被调用
mMainThread.sendActivityResult(
mToken, mEmbeddedID, requestCode, ar.getResultCode(),
ar.getResultData());
}
if (requestCode >= 0) {
// If this start is requesting a result, we can avoid making
// the activity visible until the result is received. Setting
// this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
// activity hidden during this time, to avoid flickering.
// This can only be done when a result is requested because
// that guarantees we will get information back when the
// activity is finished, no matter what happens to it.
mStartedActivity = true;
}

final View decor = mWindow != null ? mWindow.peekDecorView() : null;
if (decor != null) {
decor.cancelPendingInputEvents();
}
// TODO Consider clearing/flushing other event sources and events for child windows.
} else {
//在ActivityGroup内部的Activity调用startActivity的时候会走到这里,内部处理逻辑和上面是类似的
if (options != null) {
mParent.startActivityFromChild(this, intent, requestCode, options);
} else {
// Note we want to go through this method for compatibility with
// existing applications that may have overridden it.
mParent.startActivityFromChild(this, intent, requestCode);
}
}
}

以上是关于acitivity启动原理的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot启动原理分析

Spring Boot深入原理 - SpringApplication启动原理

iOS程序启动原理的理解

iOS程序启动原理

Android重学系列 Service 启动和绑定原理

Spring Boot:Spring Boot启动原理分析