代码是基于android 26版本分析
我们从ActivityThread这个类开始,可以看到里面有一个main方法,是不是很熟悉。它跟java一样,此方法为入口,内部代码不多,截取关键代码如下:
/**
android.app.ActivityThread
**/
public static void main(String[] args) {
//在主线程创建一个Looper,并设置给ThreadLocal,Looper内部维护了一个消息队列mQueue
Looper.prepareMainLooper();
//调用自己的构造方法创建自己的实例
ActivityThread thread = new ActivityThread();
//1
thread.attach(false);
//把自己的内部Handler对象mH赋值给sMainThreadHandler
if (sMainThreadHandler == null) {
sMainThreadHandler = thread.getHandler();
}
//Looper开启轮询
Looper.loop();
}
注释1处为关键代码,我们可以点进去看里面做了什么事情
/**
android.app.ActivityThread
**/
private void attach(boolean system) {
//通过binder机制拿到代理对象ActivityManger
final IActivityManager mgr = ActivityManager.getService();
try {
//mAppThread是ActivityThread的内部类ApplicationThread,把它设置给ActivityManger,
//实质上也是通过binder,使系统拿到AppliactionThread的代理类,从而可以操作Activity的生命周期,
//这个后面会分析到,此处实际上是app和系统进程拿到了互相的引用
mgr.attachApplication(mAppThread);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
到这里为止,系统已经可以调用acctivity的生命周期方法了,上面注释里面也已经讲到,系统会拿到我们ApplicationThread的代理类,那这个类又是什么呢,我们去看一下:
/**
android.app.ActivityThread.ApplicationThread
**/
private class ApplicationThread extends IApplicationThread.Stub {
@Override
public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
ActivityInfo info, Configuration curConfig, Configuration overrideConfig,
CompatibilityInfo compatInfo, String referrer, IVoiceInteractor voiceInteractor,
int procState, Bundle state, PersistableBundle persistentState,
List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
boolean notResumed, boolean isForward, ProfilerInfo profilerInfo) {
//...省略...
ActivityClientRecord r = new ActivityClientRecord();
sendMessage(H.LAUNCH_ACTIVITY, r);
}
可以看到,这个类继承了IApplicationThread.Stub,可以猜想到这个类是对外提供服务的,里面定义了一系列的方法,当我们启动activity时,scheduleLaunchActivity就会被调用,最终发送消息给mH这个Handler,当Handler收到消息后,又会做什么处理呢,继续跟踪过去
/**
android.app.ActivityThread.H
**/
private class H extends Handler {
public void handleMessage(Message msg) {
//...省略...
switch (msg.what) {
case LAUNCH_ACTIVITY: {
final ActivityClientRecord r = (ActivityClientRecord) msg.obj;
r.packageInfo = getPackageInfoNoCheck(
r.activityInfo.applicationInfo, r.compatInfo);
handleLaunchActivity(r, null, "LAUNCH_ACTIVITY");
} break
//...省略...
当我们收到LAUNCH_ACTIVITY这个消息后,会调用handleLaunchActivity这个方法,关键代码如下:
/**
android.app.ActivityThread
**/
private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent, String reason) {
//...省略...
// WindowManagerGlobal是一个单例类,里面会初始化windowManger
WindowManagerGlobal.initialize();
//...省略...
Activity a = performLaunchActivity(r, customIntent);
if (a != null) {
r.createdConfig = new Configuration(mConfiguration);
reportSizeConfigurations(r);
Bundle oldState = r.state;
//1
handleResumeActivity(r.token, false, r.isForward,
!r.activity.mFinished && !r.startsNotResumed, r.lastProcessedSeq, reason);
}
//...省略...
我们看到里面有一个performLaunchActivity的方法,点进去
/**
android.app.ActivityThread
**/
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
//...省略...
java.lang.ClassLoader cl = appContext.getClassLoader();
//newActivity方法通过反射实例化出来activity
activity = mInstrumentation.newActivity(
cl, component.getClassName(), r.intent);
//...省略...
//调用activity的attach方法,该方法内部会创建一个PhoneWindow
activity.attach(appContext, this, getInstrumentation(), r.token,
r.ident, app, r.intent, r.activityInfo, title, r.parent,
r.embeddedID, r.lastNonConfigurationInstances, config,
r.referrer, r.voiceInteractor, window, r.configCallback);
// ...省略...
//此处将会回调我们熟悉的activity的onCreate方法
if (r.isPersistable()) {
mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
} else {
mInstrumentation.callActivityOnCreate(activity, r.state);
}
return activity;
}
- 此处内部调用activity的attach方法,该方法内部会创建一个PhoneWindow,是Window的具体实现类,并作为activity的私有属性,并把windowManger这个系统服务设置给activty。该windowManger实质上是控制窗口行为的一个本地代理类
- 之后会有一个callActivityOnCreate的方法,该方法最终会回调我们熟悉的activity中的onCreate方法
- 到这里我们的activity已经走完了onCreate的生命周期,接下来我们来看一下handleLaunchActivity这个方法注释1处调用handleResumeActivity这个方法内部实现
/**
android.app.ActivityThread
**/
final void handleResumeActivity(IBinder token,
boolean clearHide, boolean isForward, boolean reallyResume, int seq, String reason) {
//方法内部会回调activity的onResume方法
r = performResumeActivity(token, clearHide, reason);
final Activity a = r.activity;
//拿到PhoneWindow对象
r.window = r.activity.getWindow();
//拿到DecorView对象
View decor = r.window.getDecorView();
decor.setVisibility(View.INVISIBLE);
//这个wm是一个WindowManagerImpl对象
ViewManager wm = a.getWindowManager();
WindowManager.LayoutParams l = r.window.getAttributes();
//把DecorView赋给activity
a.mDecor = decor;
l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
l.softInputMode |= forwardBit;
if (a.mVisibleFromClient) {
if (!a.mWindowAdded) {
a.mWindowAdded = true;
//WindowManagerImpl.addView方法内部最终会调用WindowManagerGlobal的addView方法
wm.addView(decor, l);
} else {
a.onWindowAttributesChanged(l);
}
}
}
... 未完....