以前看了很多,时间长了都忘了,所以还是勤快点,把看到的都记下来,算是给自己点积累。
Activity启动分为很多种情况,这里说的是打开新的应用程序第一个Activity的流程。
1. AcitivityManager产生新进程,新进程从android.app.ActivityThread.main开始运行。这里就是一般意义上的程序入口点,类似于C的main函数。 ActivityManagerService.java private final void startProcessLocked(ProcessRecord app, String hostingType, String hostingNameStr) { // Process里面通知Zygote服务,Zygote真正产生新的进程(准确说是复制一个) int pid = Process.start("android.app.ActivityThread", }
2.ActivityThread的main函数中将Looper准备起来,prepareMainLooper标志着这是应用程序主线程的Looper对象。 ActivityThread.java public static final void main(String[] args) { Looper.prepareMainLooper();
ActivityThread thread = new ActivityThread(); }
3. 接下来调用attach,参数为false,表明这不是系统进程,是给普通应用程序用使用的进程。 ActivityThread.java private final void attach(boolean system) { ViewRoot.addFirstDrawHandler(new Runnable() { }
mAppThread是ApplicationThread对象,是提供给ActivityManagerService控制ActivityThread的回调接口。
private final class ApplicationThread extends ApplicationThreadNative { public final void schedulePauseActivity(IBinder token, boolean finished,boolean userLeaving, int configChanges){ queueOrSendMessage(finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY, public final void scheduleStopActivity(IBinder token, boolean showWindow, int configChanges) { ......... } ActivityManagerService要Pause当前Activity,就会调用schedulePauseActivity想本地的消息循环中加入一个H.PAUSE_ACTIVITY的消息,然后立即返回以避免ActivityManagerService的阻塞。
4.现在又回到了ActivityManagerService中 ActivityManagerService.java // ActivityManagerService中XXXLocked函数才是真正干活的地方,XXX只是个套 public final void attachApplication(IApplicationThread thread) {
private final boolean attachApplicationLocked(IApplicationThread thread, int pid) { // 这里取出对应该Pid的ProcessRecord对象,如果取不出来,你就等着out吧 app = mPidsSelfLocked.get(pid) // 为ProcessRecord对象补充信息 app.thread = thread;
// 回调之前的ActivityThread,让它记住自己在ActivityManagerService中的相关信息,传这么大坨东西,真给力 thread.bindApplication(processName, app.instrumentationInfo != null // topRunningActivityLocked的意思没看太明白 HistoryRecord hr = topRunningActivityLocked(null); // 启动Activity realStartActivityLocked(hr, app, true, true); }
private final boolean realStartActivityLocked(HistoryRecord r, // 这里又跑到ActivityThread中去了 app.thread.scheduleLaunchActivity(new Intent(r.intent), r, System.identityHashCode(r), }
5.ActivityThread开始调度用户的Activity启动了 ActivityThread.java private final void handleLaunchActivity(ActivityRecord r, Intent customIntent) { ...... }
private final void performLaunchActivity(ActivityRecord r, Intent customIntent) { java.lang.ClassLoader cl = r.packageInfo.getClassLoader(); // 将新生的Activity与当前应用关联 activity.attach(appContext, this, getInstrumentation(), r.token, ..... }
6.Acivity与当前App关联,直到这里,平时应用所见的Activity才真正被构建 Actiivty.java
final void attach(Context context, ActivityThread aThread, Instrumentation instr, IBinder token, int ident, // 记录传入ContexImpl实例,这就是平时所见的Activity Context
7.ActivityThread ActivityThread.java private final void performLaunchActivity(ActivityRecord r, Intent customIntent) { ...... // 回调Activity::onCreate mInstrumentation.callActivityOnCreate(activity, r.state); // 记录新产生的Activity }
private final void handleLaunchActivity(ActivityRecord r, Intent customIntent) { ...... handleResumeActivity(r.token, false, r.isForward); }
final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) {
}
public final ActivityRecord performResumeActivity(IBinder token, boolean clearHide) { if (r.pendingIntents != null) { }
到这里,Activity从产生到初始化的过程就全部结束了。之后就是等待用户界面的消息,根据消息进行相应的处理。整个过程中,在ActivityManagerServer、ActivityThread的互相协作下构建出Activity,并在相应的时机回调Activity的相应接口,完成Activity的初始化。 |
|
来自: joy_chen > 《activity》