You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
5.0 KiB
5.0 KiB
说说 Activity 的启动流程
- 启动 Activity 会经历哪些生命周期回调
- 冷启动大致流程,涉及哪些组件,通信过程是怎么样的?
- Activity 启动过程中,生命周期回调的原理?
启动流程
startActivity ->
ActivityManagerNative.getDefault().startActivity ->
mRemote.transact(START_ACTIVITY_TRANSACTION, data, reply, 0);
@Override
public boolean onTransact(int code, Parcel data, Parcel reply, int flags) {
switch(code) {
case START_ACTIVITY_TRANSACTION: {
startActivity(app, callingPackage, intent, ...);
}
}
}
// AMS
void startSpecialActivityLocked(ActivityRecord r, ...) {
// 查询组件对应的进程是否已经启动
ProcessRecord app = mService.getProcessRecordLocked(r.processName, ...);
if(app!=null&&app.thread!=null){
realStartActivity(r, app, ...);
return;
}
// 启动进程
mService.startProcessLocked(r.processName, ...);
}
private final void startProcessLocked(ProcessRecord app) {
// 进程启动完成后,执行的入口函数的 Java 类
if(entryPoint == null) entryPoint = "android.app.ActivityThread";
Process.ProcessStartResult startResult = Process.start(entryPoint, );
synchronized(mPidsSelfLocked) {
mPidsSelfLocked.put(startResult.pid, app);
Message msg = mHandle.obtainMessage(PROC_START_TIMEOUT_MSG);
msg.obj = app;
mHandle.sendMessageDelayed(msg, PROC_START_TIMEOUT);
}
}
public static final ProcessStartResult start(final String processClass, ){
return startViaZygote(processClass,);
}
ProcessStartResult startViaZygote(final String processClass, ){
return zygoteSendArgsAndGetResult(
openZygoteSocketIfNeeded(abi), argsForZygote);
}
boolean runOnce() {
// 读取 Zygote 发送过来的参数列表
String[] args = readArgumentList();
// 创建应用进程
int pid = Zygote.forkAndSpecialize(...);
if(pid == 0) {
// 子进程,执行 ActivityThread#main 函数
handleChildProc(...);
return true;
} else {
// 父进程把 pid 通过 Socket 在写回去
return handlePArentProc(pid, ...);
}
}
// ActivityThread
public static void main(String[] args) {
Looper.prepareMainLooper();
ActivityThread thread = new ActivityThread();
thread.attach(false);
Looper.loop();
throw new RuntimeException("");
}
private void attach(boolean system) {
IActivityManager mgr = ActivityManagerNative.getDefault();
mgr.attachApplication(mAppThread);
}
boolean attachApplicationLocked(IApplicationThread thread, ...) {
ProcessRecord app = mPidsSelfLocked.get(pid);
// IPC 调用,回到应用进程
thread.bindApplication(...);
// 以下处理挂起的应用组件
mStackSupervisor.attachApplicationLocked(app);
mServices.attachApplicationLocked(app, processName);
sendPendingBroadcastsLocked(app);
}
boolean attachApplicationLocked(ProcessRecord app) {
// stack: mFocusedStack,
ActivityRecord hr = stack.topRunningActivityLocked(null);
realStartActivityLocked(hr, app, true, true);
}
final boolean realStartActivityLocked(ActivityRecord r, ) {
// ProcessRecord.ApplicationThread.scheduleLaunchActivity
app.thread.scheduleLaunchActivity(new Intent(r.intent), ...);
}
@Override
public final void scheduleLaunchActivity(Intent intent, IBinder token, ){
ActivityClientRecord r = new ActivityClientRecord();
// mH,sendMessage,丢到主线程的 Handler
sendMessage(H.LAUNCH_ACTIVITY, r);
}
// 主线程处理
final ActivityClientRecord r = (ActivityClientRecord)msg.obj;
// 生成 LoadedApk 对象
r.packageInfo = getPackageInfoNoCheck(...);
handleLaunchActivity(r, null);
private void handleLaunchActivity(ActivityClientRecord r, ...) {
Activity a = performLaunchActivity(r, customIntent);
if(a!=null) {
handleResumeActivity(r.token, false, ...);
}
}
private Activity performLaunchActivity(ActivityClientRecord r, ...) {
Activity activity = mInstrumentation.newActivity(...);
Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Context appContext = createBaseContextForActivity(r, activity);
// activity.attach
activity.attach(appContext, ...);
// activity.onCreate
mInstrumentation.callActivityOnCreate(activity, r.state);
// activity.onStart
activity.performStart();
return activity;
}
final void handleResumeActivity(IBinder token) {
ActivityClientRecord r = performResumeActivity(...);
//...
}
总结
-
创建 Activity 对象
首先通过 ClassLoder 加载 LoadedApk 里的类生成 Activity 对象
-
准备好 Application
Application 在应用进程启动时就已经创建好了,这一步直接拿即可
-
创建 ContextImpl
-
attach 上下文
-
生命周期回调
说说 Activity 的启动流程
- 启动 Activity 要向 AMS 发起 binder 调用
- Activity 所在进程是怎么启动的?
- 应用 Activity 的生命周期回调原理