各位看官好,本文是Android Framework之Activity启动流程的第三篇,本篇将分析Activity生命周期的回调,新世界的大门就在眼前,走起。
第一篇:Android Framework之Activity启动流程(一)
第二篇:Android Framework之Activity启动流程(二)
执行完 ApplicationThread# handleBindApplication () 之后,这时候新进程已经启动。
回到AMS# attachApplicationLocked(),代码逻辑来到了上篇文章的注释5处,调用了ActivityStackSupervisor# attachApplicationLocked,在里面又执行了realStartActivityLocked(),这样就回到了第一篇末尾所说到的第二种情况,接下来分析第二种情况。
ApplicationThread#scheduleLaunchActivity()
1 2 3 4
| public final void scheduleLaunchActivity(){ sendMessage(H.LAUNCH_ACTIVITY, r); }
|
sendMessage内部使用了Hanlder,发送了一个H.LAUNCH_ACTIVITY类型的消息,然后调用handleLaunchActivity()->performLaunchActivity()方法。
ActivityThread#handleLaunchActivity()
看看这两个方法到底干了什么事。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent, String reason) { unscheduleGcIdler(); handleConfigurationChanged(null, null); if (!ThreadedRenderer.sRendererDisabled) { GraphicsEnvironment.earlyInitEGL() } WindowManagerGlobal.initialize(); Activity a = performLaunchActivity(r, customIntent);
if (a != null) { r.createdConfig = new Configuration(mConfiguration); reportSizeConfigurations(r); Bundle oldState = r.state; handleResumeActivity(r.token, false, r.isForward,!r.activity.mFinished && !r.startsNotResumed, r.lastProcessedSeq, reason); } }
|
参数列表中的ActivityClientRecord可以看做是一个Activity的实例,同时还包括了其他一些属性,主要是对Activity状态进行记录。
handleLaunchActivity() 主要有两个操作:
1.注释9:performLaunchActivity()
该方法主要是调用Activity的onCreate(),onStart(),onRestoreInstance(),onPostCreate()生命周期
2.注释10:handleResumeActivity()
该方法会调用Activity的onResume()生命周期
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) { ActivityInfo aInfo = r.activityInfo; if (r.packageInfo == null) { r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo, Context.CONTEXT_INCLUDE_CODE); } ComponentName component = r.intent.getComponent(); if (component == null) { component = r.intent.resolveActivity( mInitialApplication.getPackageManager()); r.intent.setComponent(component); } if (r.activityInfo.targetActivity != null) { component = new ComponentName(r.activityInfo.packageName, r.activityInfo.targetActivity); }
ContextImpl appContext = createBaseContextForActivity(r); Activity activity = null; try { java.lang.ClassLoader cl = appContext.getClassLoader(); activity = mInstrumentation.newActivity( cl, component.getClassName(), r.intent); StrictMode.incrementExpectedActivityCount(activity.getClass()); r.intent.setExtrasClassLoader(cl); r.intent.prepareToEnterProcess(); if (r.state != null) { r.state.setClassLoader(cl); } } catch (Exception e) { } try{ Application app = r.packageInfo.makeApplication(false, mInstrumentation); if (activity != null) { 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); } …… if (r.isPersistable()) { mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState); } else { mInstrumentation.callActivityOnCreate(activity, r.state); } r.activity = activity; r.stopped = true; if (!r.activity.mFinished) { activity.performStart(); r.stopped = false; }
if (!r.activity.mFinished) { if (r.isPersistable()) { if (r.state != null || r.persistentState != null) { mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state, r.persistentState); } } else if (r.state != null) { mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state); } } if (!r.activity.mFinished) { activity.mCalled = false; if (r.isPersistable()) { mInstrumentation.callActivityOnPostCreate(activity, r.state, r.persistentState); } else { mInstrumentation.callActivityOnPostCreate(activity, r.state); } if (!activity.mCalled) { throw new SuperNotCalledException( "Activity " + r.intent.getComponent().toShortString() + " did not call through to super.onPostCreate()"); } } r.paused = true; mActivities.put(r.token, r); } catch(SuperNotCalledException e){ …… } return activity; }
|
ActivityThread#handleResumeActivity()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward, boolean reallyResume, int seq, String reason) { ActivityClientRecord r = mActivities.get(token); r = performResumeActivity(token, clearHide, reason); if (r != null) { final Activity a = r.activity; final int forwardBit = isForward ? WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0; if (r.window == null && !a.mFinished && willBeVisible) { …… ViewManager wm = a.getWindowManager(); …… wm.addView(decor, l); } …… wm.updateViewLayout(decor, l); …… if (reallyResume) { try { ActivityManager.getService().activityResumed(token); } catch (RemoteException ex) { throw ex.rethrowFromSystemServer(); } } else { try { ActivityManager.getService() .finishActivity(token, Activity.RESULT_CANCELED, null, Activity.DONT_FINISH_TASK_WITH_ACTIVITY); } } } }
|
该方法主要是回调Activity的onResume()方法(实际上是调用Instrumentation.callActivityOnResume(this)来执行),并将DecorView添加到WindowManager中,这里的WindowManager是a.getWindowManager()得到的,其实现是WindowManagerImpl,这步操作在onResume()方法之后执行。
至此,Activity的启动流程分析完毕。
看完之后感觉如何,是否恍然大悟?
总结
1、 startActivity()通过Instrumentation向AMS进程发起startActivity()请求
2、 AMS收到启动请求后由ActivityStarter处理Flags和Intent信息,然后再由ActivityStackSupervisor和ActivityStack处理Task和Stack流程
3、 在ActivityStackSupervisor中判断app进程是否存在,若不存在则请求AMS进程创建新进程,app进程存在的情况请直接跳到第7步
4、 AMS进程通过Socket方式请求Zygote fork新进程
5、 在新进程里创建ActivityThread(主线程)并开启Looper循环,同时将ApplicationThread绑定到AMS
6、 AMS回调ApplicationThread的bindApplication()方法将自身与新进程绑定,并在ActivityThread的handleBindApplication()方法中创建应用的Application
7、 Application创建成功后,AMS调用ActivityStackSupervisor# realStartActivityLocked向ActivityThread发送创建Activity请求
8、 ActivityThread利用ClassLoader去加载Activity,并回调其生命周期方法。