Activity
启动过程可以分为两种:一种是根activity
的启动过程,另一种是普通activity
启动过程。
根activity
指的是应用程序启动的第一个activity,因此也可以理解为应用程序的启动过程。普通activity
除根activity
以外的其他activity
。
activity
启动涉及4个进程:Zygote进程,Launcher进程、AMS所在的进程(systemServer进程)与应用程序进程
activity
启动只涉及AMS所在进程与应用程序进程。Launcher
进程将启动Activity
的请求以Binder
的方式发送给了AMS
。AMS
接收到启动请求后,判断根activity
所需的应用程序进程是否存在并启动,不存在以Socket
方式请求Zygote
进程fork
新进程。同时交付ActivityStarter
处理Intent
和Flag
等信息,然后再交给ActivityStackSupervisior/ActivityStack
处理Activity
进栈相关流程。Zygote
接收到新进程创建请求后fork
出新进程。ActivityThread
对象,新创建的进程就是应用的主线程,在主线程里开启Looper
消息循环,开始处理创建Activity
。ActivityThread
利用ClassLoader
去加载Activity
、创建Activity
实例,并回调Activity的onCreate()
方法。这样便完成了Activity的启动。zygote
比 service manager
先启动;从这个意义上,他没有 service manager
可以注册,所以没法用 binder
;init
进程启动的,就算先启动 service manager
,也不能保存 zygote 起来的时候 service manger
就已经初始化好了(这就需要额外的同步,太麻烦);socket
的所有者是 root
,group
是 system
,只有系统权限的用户才能读写,这又多了一个安全保障(注意,这个 socket
是 Unix
域 socket
,不是 internet
域 socket);ActivityManagerServices
,简称AMS,服务端对象,负责系统中所有Activity
的生命周期。ActivityThread
:App
的真正入口。当开启App
之后,调用main()
开始运行,开启消息循环队列,这就是传说的UI线程或者叫主线程。与ActivityManagerService
一起完成Activity的管理工作。ApplicationThread
:用来实现ActivityManagerServie
与ActivityThread
之间的交互。在ActivityManagerSevice
需要管理相关Application
中的Activity
的生命周期时,通过ApplicationThread
的代理对象与ActivityThread
通信。ApplicationThreadProxy
:是ApplicationThread
在服务器端的代理,负责和客户端的ApplicationThread
通信。AMS就是通过该代理与ActivityThread
进行通信的。Instrumentation
:监控应用与系统相关的交互行为,每一个应用程序只有一个Instrumetation
对象,每个Activity内都有一个对该对象的引用,Instrumentation
可以理解为应用进程的管家,ActivityThread要创建或暂停某个Activity时,都需要通过Instrumentation来进行具体的操作。ActivityStack
:Activity在AMS的栈管理,用来记录经启动的Activity的先后关系,状态信息等。通过ActivtyStack决定是否需要启动新的进程。ActivityRecord
:ActivityStack
的管理对象,每个Acivity在AMS对应一个ActivityRecord,来记录Activity状态以及其他的管理信息。其实就是服务器端的Activit对象的映像。TaskRecord
:AMS抽象出来的一个“任务”的概念,是记录ActivityRecord
的栈,一个Task
包含若干个ActivityRecord
。AMS
用TaskRecord
确保Activity
启动和退出的顺序。ActivityStarter
:Activity启动的控制器,处理Intent与Flag对Activity
启动的影响,具体说来有:
ActivityStackSupervisior
:这个类的作用你从它的名字就可以看出来,它用来管理任务栈。Activity
调用startActivity
最终都会调用startActivityForResult
方法
public void startActivityForResult(@RequiresPermission Intent intent, int requestCode,
@Nullable Bundle options) {
//判读当前Activity是否为空,为空调用Instrumentation的execStartActivity进行创建
if (mParent == null) {
options = transferSpringboardActivityOptions(options);
Instrumentation.ActivityResult ar =
mInstrumentation.execStartActivity(
this, mMainThread.getApplicationThread(), mToken, this,
intent, requestCode, options);
if (ar != null) {
mMainThread.sendActivityResult(
mToken, mEmbeddedID, requestCode, ar.getResultCode(),
ar.getResultData());
}
......
}
execStartActivity
方法
public ActivityResult execStartActivity(
Context who, IBinder contextThread, IBinder token, Activity target,
Intent intent, int requestCode, Bundle options) {
IApplicationThread whoThread = (IApplicationThread) contextThread;
Uri referrer = target != null ? target.onProvideReferrer() : null;
if (referrer != null) {
intent.putExtra(Intent.EXTRA_REFERRER, referrer);
}
if (mActivityMonitors != null) {
synchronized (mSync) {
final int N = mActivityMonitors.size();
for (int i=0; i<N; i++) {
final ActivityMonitor am = mActivityMonitors.get(i);
ActivityResult result = null;
if (am.ignoreMatchingSpecificIntents()) {
result = am.onStartActivity(intent);
}
if (result != null) {
am.mHits++;
return result;
} else if (am.match(who, null, intent)) {
am.mHits++;
if (am.isBlocking()) {
return requestCode >= 0 ? am.getResult() : null;
}
break;
}
}
}
}
try {
intent.migrateExtraStreamToClipData();
intent.prepareToLeaveProcess(who);
int result = ActivityManager.getService()
.startActivity(whoThread, who.getBasePackageName(), intent,
intent.resolveTypeIfNeeded(who.getContentResolver()),
token, target != null ? target.mEmbeddedID : null,
requestCode, 0, null, options);
checkStartActivityResult(result, intent);
} catch (RemoteException e) {
throw new RuntimeException("Failure from system", e);
}
return null;
}
启动Activity最终调用的是ActivityManager.getService().startActivity
方法,而ActivityManager.getService()
返回的是IActivityManager
对象,它是一个Binder
对象。
ActivityManagerService
继承自IActivityManager.Stub
对象。
getService
方法如下:
public static IActivityManager getService() {
//获取IActivityManagerSingleton实例
return IActivityManagerSingleton.get();
}
//创建IActivityManagerSingleton实例对象
private static final Singleton<IActivityManager> IActivityManagerSingleton =
new Singleton<IActivityManager>() {
@Override
protected IActivityManager create() {
final IBinder b = ServiceManager.getService(Context.ACTIVITY_SERVICE);
final IActivityManager am =
//将服务端对象转换成客户端所需的AIDL接口对象,由于ActivityManagerService继承自IActivityManager.Stub对象。所以这里就返回了ActivityManagerService对象实例
IActivityManager.Stub.asInterface(b);
return am;
}
};
所以ActivityManager.getService().startActivity
方法实质上调用的是ActivityManagerService的startActivity
方法。
checkStartActivityResult
方法是用来检查Activity启动的结果,比如没有在AndroidManifest.xml
中注册这个Activity实例对象。
public static void checkStartActivityResult(int res, Object intent) {
if (!ActivityManager.isStartResultFatalError(res)) {
return;
}
switch (res) {
case ActivityManager.START_INTENT_NOT_RESOLVED:
case ActivityManager.START_CLASS_NOT_FOUND:
if (intent instanceof Intent && ((Intent)intent).getComponent() != null)
throw new ActivityNotFoundException(
"Unable to find explicit activity class "
+ ((Intent)intent).getComponent().toShortString()
+ "; have you declared this activity in your AndroidManifest.xml?");
throw new ActivityNotFoundException(
"No Activity found to handle " + intent);
case ActivityManager.START_PERMISSION_DENIED:
throw new SecurityException("Not allowed to start activity "
+ intent);
case ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT:
throw new AndroidRuntimeException(
"FORWARD_RESULT_FLAG used while also requesting a result");
case ActivityManager.START_NOT_ACTIVITY:
throw new IllegalArgumentException(
"PendingIntent is not an activity");
case ActivityManager.START_NOT_VOICE_COMPATIBLE:
throw new SecurityException(
"Starting under voice control not allowed for: " + intent);
case ActivityManager.START_VOICE_NOT_ACTIVE_SESSION:
throw new IllegalStateException(
"Session calling startVoiceActivity does not match active session");
case ActivityManager.START_VOICE_HIDDEN_SESSION:
throw new IllegalStateException(
"Cannot start voice activity on a hidden session");
case ActivityManager.START_ASSISTANT_NOT_ACTIVE_SESSION:
throw new IllegalStateException(
"Session calling startAssistantActivity does not match active session");
case ActivityManager.START_ASSISTANT_HIDDEN_SESSION:
throw new IllegalStateException(
"Cannot start assistant activity on a hidden session");
case ActivityManager.START_CANCELED:
throw new AndroidRuntimeException("Activity could not be started for "
+ intent);
default:
throw new AndroidRuntimeException("Unknown error code "
+ res + " when starting " + intent);
}
}
ActivityManagerService的startActivity
方法。
@Override
public final int startActivity(IApplicationThread caller, String callingPackage,
Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,
int startFlags, ProfilerInfo profilerInfo, Bundle bOptions) {
return startActivityAsUser(caller, callingPackage, intent, resolvedType, resultTo,
resultWho, requestCode, startFlags, profilerInfo, bOptions,
UserHandle.getCallingUserId());
}
@Override
public final int startActivityAsUser(IApplicationThread caller, String callingPackage,
Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,
int startFlags, ProfilerInfo profilerInfo, Bundle bOptions, int userId) {
enforceNotIsolatedCaller("startActivity");
userId = mUserController.handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(),
userId, false, ALLOW_FULL_ONLY, "startActivity", null);
// TODO: Switch to user app stacks here.
return mActivityStarter.startActivityMayWait(caller, -1, callingPackage, intent,
resolvedType, null, null, resultTo, resultWho, requestCode, startFlags,
profilerInfo, null, null, bOptions, false, userId, null, "startActivityAsUser");
}
调用了startActivityMayWait方法进入到了ActivityStarter类中,而startActivityMayWait中又调用了startActivityLocked方法
final int startActivityMayWait(IApplicationThread caller, int callingUid,
String callingPackage, Intent intent, String resolvedType,
IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
IBinder resultTo, String resultWho, int requestCode, int startFlags,
ProfilerInfo profilerInfo, WaitResult outResult,
Configuration globalConfig, Bundle bOptions, boolean ignoreTargetSecurity, int userId,
TaskRecord inTask, String reason) {
final ActivityRecord[] outRecord = new ActivityRecord[1];
int res = startActivityLocked(caller, intent, ephemeralIntent, resolvedType,
aInfo, rInfo, voiceSession, voiceInteractor,
resultTo, resultWho, requestCode, callingPid,
callingUid, callingPackage, realCallingPid, realCallingUid, startFlags,
options, ignoreTargetSecurity, componentSpecified, outRecord, inTask,
reason);
}
ActivityStarter类中startActivityMayWait—>startActivityLocked->startActivity->startActivityUnchecked然后调用resumeFocusedStackTopActivityLocked方法进入到了ActivityStackSupervisior
类中。
boolean resumeFocusedStackTopActivityLocked(
ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
if (!readyToResume()) {
return false;
}
if (targetStack != null && isFocusedStack(targetStack)) {
//当期ActivityStack不为空就调用resumeTopActivityUncheckedLocked方法
return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
}
final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
if (r == null || r.state != RESUMED) {
mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
} else if (r.state == RESUMED) {
// Kick off any lingering app transitions form the MoveTaskToFront operation.
mFocusedStack.executeAppTransition(targetOptions);
}
return false;
}
当期ActivityStack
不为空就调用resumeTopActivityUncheckedLocked
方法,在这个方法中又调用了resumeTopActivityInnerLocked
方法
mStackSupervisor.startSpecificActivityLocked(next, true, true);
void startSpecificActivityLocked(ActivityRecord r,
boolean andResume, boolean checkConfig) {
// Is this activity's application already running?
ProcessRecord app = mService.getProcessRecordLocked(r.processName,
r.info.applicationInfo.uid, true);
r.getStack().setLaunchTime(r);
if (app != null && app.thread != null) {
try {
if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
|| !"android".equals(r.info.packageName)) {
app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
mService.mProcessStats);
}
realStartActivityLocked(r, app, andResume, checkConfig);
return;
} catch (RemoteException e) {
Slog.w(TAG, "Exception when starting activity "
+ r.intent.getComponent().flattenToShortString(), e);
}
}
内部调用realStartActivityLocked
开始真正的启动Activity
final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
boolean andResume, boolean checkConfig) throws RemoteException {
......
app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
System.identityHashCode(r), r.info,
// TODO: Have this take the merged configuration instead of separate global
// and override configs.
mergedConfiguration.getGlobalConfiguration(),
mergedConfiguration.getOverrideConfiguration(), r.compat,
r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
r.persistentState, results, newIntents, !andResume,
mService.isNextTransitionForward(), profilerInfo);
.....
}
这里的app.thread.
就是IApplicationThread
,它继承自IInterface
,是一个Binder对象,包含大量启动与停止Activity接口,还包含服务。
private class ApplicationThread extends IApplicationThread.Stub
ActivityThread
内部类ApplicationThread
继承IApplicationThread.Stub对
象,所以app.thread.scheduleLaunchActivity
方法调用实质就是调用ApplicationThread
的scheduleLaunchActivity
方法。
@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) {
updateProcessState(procState, false);
ActivityClientRecord r = new ActivityClientRecord();
r.token = token;
r.ident = ident;
r.intent = intent;
r.referrer = referrer;
r.voiceInteractor = voiceInteractor;
r.activityInfo = info;
r.compatInfo = compatInfo;
r.state = state;
r.persistentState = persistentState;
r.pendingResults = pendingResults;
r.pendingIntents = pendingNewIntents;
r.startsNotResumed = notResumed;
r.isForward = isForward;
r.profilerInfo = profilerInfo;
r.overrideConfig = overrideConfig;
updatePendingConfiguration(curConfig);
sendMessage(H.LAUNCH_ACTIVITY, r);
}
scheduleLaunchActivity
做的事很简单,就是通过handler发送一个启动Activity的消息。
case LAUNCH_ACTIVITY: {
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
final ActivityClientRecord r = (ActivityClientRecord) msg.obj;
r.packageInfo = getPackageInfoNoCheck(
r.activityInfo.applicationInfo, r.compatInfo);
handleLaunchActivity(r, null, "LAUNCH_ACTIVITY");
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
} break;
然后调用handleLaunchActivity
来处理
private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent, String reason) {
Activity a = performLaunchActivity(r, customIntent);
......
}
内部又调用performLaunchActivity
来创建Activity
首先获取Activity的组件信息
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);
}
然后通过Instrumentation.newActivity
使用ClassLoader
类加载器去创建Activity实例对象
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) {
if (!mInstrumentation.onException(activity, e)) {
throw new RuntimeException(
"Unable to instantiate activity " + component
+ ": " + e.toString(), e);
}
}
再通过Application app = r.packageInfo.makeApplication(false, mInstrumentation);
创建Application对象实例,方法内部进行判断是否已经创建,创建过后就不会再次创建。
创建ContextImpl
对象并通过Activity的attach
方法进行一些重要的数据初始化操作,ContextImpl
是一种重要的数据结构,是Context的具体实现,Context的大部分逻辑都是通过ContextImpl来完成的。它是通过Activity的attach方法进行与Activity关联的。
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方法完成Activity的创建
activity.mCalled = false;
if (r.isPersistable()) {
mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
} else {
mInstrumentation.callActivityOnCreate(activity, r.state);
}
}
public void callActivityOnCreate(Activity activity, Bundle icicle,
PersistableBundle persistentState) {
prePerformCreate(activity);
activity.performCreate(icicle, persistentState);
postPerformCreate(activity);
}
Activity的onCreate方法
final void performCreate(Bundle icicle, PersistableBundle persistentState) {
mCanEnterPictureInPicture = true;
restoreHasCurrentPermissionRequest(icicle);
if (persistentState != null) {
onCreate(icicle, persistentState);
} else {
onCreate(icicle);
}
mActivityTransitionState.readState(icicle);
mVisibleFromClient = !mWindow.getWindowStyle().getBoolean(
com.android.internal.R.styleable.Window_windowNoDisplay, false);
mFragments.dispatchActivityCreated();
mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
}