本文主要讲解activity的生命周期,启动模式和工作过程
1.生命周期,主要分为正常情况下的生命周期和异常情况下的生命周期
正常情况下的生命周期:
谈到生命周期,不得不提到下面的图片,相比很多人都已经很了解了,就不多做介绍了
但我还要提一下
1.如果activity采用了透明的主题,那么当前activity、不会调用onStop方法
2.A启动B,B是在A调用了onPause之后才会启动,onCreat-onStart-onResume,然后调用A的onStop方法。所以我们不能在onPause内做重量级的操作,这样会影响新界面的开启,如果必须要做最好在onStop中,但也尽量少做。
异常情况下的生命周期:
1.资源相关的系统配置发生改变导致Activity被杀死后重建(横竖屏切换)
2.资源内存不足导致activity被杀死
onSaveInstanceState这个方法是在onDestory之前调用的和onPause没有关系,但实际基本都是onPause之后,onStop之前调用
onRestoreInstanceState是在onStart之后,onResume之前调用
2.activity的启动模式
standard:标准模式,Activity默认会进入启动它的Activity所属的任务栈中,也就是谁启动了这个Activity,那么这个Activity就运行在启动它的那个Activity所在的栈中。
当我们用ApplicationContext去启动standard模式的activity时会报错,因为非Activity类型的Context(如:ApplicationCotext)并没有任务栈。
解决办法:使用FLAG_ACTIVITY_NEW_TASK标记,其实也就是在代码中改变了activity的启动模式,改成了singleTask的启动模式了。
singleTop:栈顶复用模式,ABC再开启C,还是ABC而不是ABCC。
singleTask:栈内复用模式,如果A是singleTask模式,那么启动A时,先回判断是否存在A需要的栈,不存在则创建一个任务栈并创建A实例放入栈中;存在则判断栈内是否存在A的实例,不存在则创建A实例放入栈中,存在将A调到栈顶并调用它的onNewIntent方法。(将栈内A上面的Activity全部清除)
singleInstance:单实例模式,栈内只有一个此模式的activity。
最后说一下taskAffinity(任务栈)和Activity的Flags:
taskAffinity一般情况下会和singleTask或者allowTaskReparenting配合使用,用于给activity设置任务栈,默认的任务栈是包名;
allowTaskReparenting用来标记Activity能否从启动的Task移动到taskAffinity指定的Task,默认是继承至application中的allowTaskReparenting=false,如果为true,则表示可以更换;false表示不可以。
引用网上的解释例子:
一般来说,当Activity启动后,它就与启动它的Task关联,并且在那里耗尽它的整个生命周期。当当前的Task不再显示时,你可以使用这个特性来强制Activity移动到有着affinity的Task中。例如,如果e-mail中包含一个web页的链接,点击它就会启动一个Activity来显示这个页面。这个Activity是由Browser应用程序定义的,但是,现在它作为e-mail Task的一部分。如果它重新宿主到Browser Task里,当Browser下一次进入到前台时,它就能被看见,并且,当e-mail Task再次进入前台时,就看不到它了。
Activity的Flags(常用的):
1.FLAG_ACTIVITY_NEW_TASK
相当于singleTask
2.FLAG_ACTIVITY_CLEAR_TOP 一般和FLAG_ACTIVITY_NEW_TASK配合使用,清除上面的activity
3.FLAG_ACTIVITY_SINGLE_TOP 相当于singleTop
4.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS 就是长按android的home键,然后清除刚才看的应用,这个属性的作用恰恰就是让你在长按home键的时候在弹出的应用列表中隐藏你的应用,达到隐藏应用程序进行的目的。(和android:excludeFromRecents="true"功能一样)
本人测试了一下:
android:excludeFromRecents="true"android:launchMode="singleInstance"
这样才管用,可能是excludefromrecents要配合singleInstance才可以(不知道是不是这样的,不太确认)
3.activity的启动过程
activity是一种展示型组件,用于向用户直接展示一个界面,并且可以根据用户输入的信息进行交互。
我们通常是通过startActivity或者startActivityForResult来启动一个新activity,而它们又有好几种重载方法,但最终都会调用到Activity类中startActivityForResult的方法:
(1)在Activity.java文件中:
@Override
public void startActivity(Intent intent, Bundle options) {
if (options != null) {
startActivityForResult(intent, -1, options);
} else {
// Note we want to go through this call for compatibility with
// applications that may have overridden the method.
startActivityForResult(intent, -1);
}
}
public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
/*
* mParent代表的是ActivityGroup,
* ActivityGroup最开始被用来在一个界面中嵌入多个Activity,
* 但是其在API13中已经被废弃了,系统推荐采用Fragment来代替ActivityGroup。
* */
if (mParent == null) {
/*
* mMainThread.getApplicationThread()这个参数,它的类型是ApplicationThread,
* ApplicationThread是ActivityThread的一个内部类,
* 在后面的分析中可以发现,ApplicationThread和ActivityThread在Activity的启动过程中发挥着很重要的作用。
* */
Instrumentation.ActivityResult ar =
/*
* 所以说Activity的启动过程转移到了Instrumentation中的execStartActivity方法:
* */
mInstrumentation.execStartActivity(
this, mMainThread.getApplicationThread(), mToken, this,
intent, requestCode, options);
if (ar != null) {
mMainThread.sendActivityResult(
mToken, mEmbeddedID, requestCode, ar.getResultCode(),
ar.getResultData());
}
if (requestCode >= 0) {
// If this start is requesting a result, we can avoid making
// the activity visible until the result is received. Setting
// this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
// activity hidden during this time, to avoid flickering.
// This can only be done when a result is requested because
// that guarantees we will get information back when the
// activity is finished, no matter what happens to it.
mStartedActivity = true;
}
final View decor = mWindow != null ? mWindow.peekDecorView() : null;
if (decor != null) {
decor.cancelPendingInputEvents();
}
// TODO Consider clearing/flushing other event sources and events for child windows.
} else {
if (options != null) {
mParent.startActivityFromChild(this, intent, requestCode, options);
} else {
// Note we want to go through this method for compatibility with
// existing applications that may have overridden it.
mParent.startActivityFromChild(this, intent, requestCode);
}
}
}
(2)Instrumentation的execStartActivity方法:在Instrumentation.java文件中。
public ActivityResult execStartActivity(
Context who, IBinder contextThread, IBinder token, Activity target,
Intent intent, int requestCode, Bundle options) {
IApplicationThread whoThread = (IApplicationThread) contextThread;
if (mActivityMonitors != null) {
synchronized (mSync) {
final int N = mActivityMonitors.size();
for (int i=0; i<N; i++) {
final ActivityMonitor am = mActivityMonitors.get(i);
if (am.match(who, null, intent)) {
am.mHits++;
if (am.isBlocking()) {
return requestCode >= 0 ? am.getResult() : null;
}
break;
}
}
}
}
try {
intent.migrateExtraStreamToClipData();
intent.prepareToLeaveProcess();
/*
* 所以启动Activity的真正实现由ActivityManagerNative.getDefault().startActivity方法来完成。
*
* ActivityManagerService继承自ActivityManagerNative,
* 而ActivityManagerNative继承自Binder并实现了IActivityManager这个Binder接口,
* 因此ActivityManagerService也是一个Binder,它是IActivityManager的具体实现。
*
* 由于ActivityManagerNative.getDefault()其实是一个IActivityManager类型的Binder对象,
* 因此它的具体实现是ActivityManagerService。
* 所以说Activity的启动过程又转移到了ActivityManagerService中,
* 然后再去看ActivityManagerService的startActivity方法。
* */
int result = ActivityManagerNative.getDefault()
.startActivity(whoThread, who.getBasePackageName(), intent,
intent.resolveTypeIfNeeded(who.getContentResolver()),
token, target != null ? target.mEmbeddedID : null,
requestCode, 0, null, null, options);
/*
* 检查启动Activity的结果:
* */
checkStartActivityResult(result, intent);
} catch (RemoteException e) {
}
return null;
}
在上面的代码中可以看到这一句:checkStartActivityResult(result, intent);我们去看看这个方法的源码:
static void checkStartActivityResult(int res, Object intent) {
if (res >= ActivityManager.START_SUCCESS) {
return;
}
switch (res) {
case ActivityManager.START_INTENT_NOT_RESOLVED:
case ActivityManager.START_CLASS_NOT_FOUND:
if (intent instanceof Intent && ((Intent)intent).getComponent() != null)
/*
* 这个异常错误抛出最常见了。
* 如果没有在AndroidManifest中注册Activity,就会抛出此异常。
* */
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");
default:
throw new AndroidRuntimeException("Unknown error code "
+ res + " when starting " + intent);
}
}
(3)在ActivityManagerNative.java文件中:
ActivityManagerNative.getDefault实际上就是ActivityManagerService,因此Activity的启动过程转移到了ActivityManagerService中。
/*
* 在Instrumentation的execStartActivity中用ActivityManagerNative的getDefault来获取一个IActivityManager的对象,
* 而且这个IActivityManager的对象其实是一个Binder对象,它的具体实现是ActivityManagerService。
* */
static public IActivityManager getDefault() {
return gDefault.get();
}
/*
* 在ActivityManagernative中,ActivityManagerService这个Binder对象采用单例模式对外提供,
* Singleton是一个单例的封装类,
* 第一次调用它的get方法时它会通过create方法来初始化ActivityManagerService这个Binder对象,
* 在后续的调用中则直接返回之前创建的对象。
* */
private static final Singleton<IActivityManager> gDefault = new Singleton<IActivityManager>() {
protected IActivityManager create() {
IBinder b = ServiceManager.getService("activity");
if (false) {
Log.v("ActivityManager", "default service binder = " + b);
}
/*
* 将Binder对象转换成对应IActivityManager的AIDL接口对象:
* */
IActivityManager am = asInterface(b);
if (false) {
Log.v("ActivityManager", "default service = " + am);
}
return am;
}
};
(4)ActivityManagerService的startActivity方法,在ActivityManagerService.java文件中:
@Override
public final int startActivity(IApplicationThread caller, String callingPackage,
Intent intent, String resolvedType, IBinder resultTo,
String resultWho, int requestCode, int startFlags,
String profileFile, ParcelFileDescriptor profileFd, Bundle options) {
/*
* Activity的启动过程又转移到了startActivityAsUser方法中,再进去看看:
* */
return startActivityAsUser(caller, callingPackage, intent, resolvedType, resultTo,
resultWho, requestCode,
startFlags, profileFile, profileFd, options, UserHandle.getCallingUserId());
}
@Override
public final int startActivityAsUser(IApplicationThread caller, String callingPackage,
Intent intent, String resolvedType, IBinder resultTo,
String resultWho, int requestCode, int startFlags,
String profileFile, ParcelFileDescriptor profileFd, Bundle options, int userId) {
enforceNotIsolatedCaller("startActivity");
userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId,
false, true, "startActivity", null);
// TODO: Switch to user app stacks here.
return mStackSupervisor.startActivityMayWait(caller, -1, callingPackage, intent, resolvedType,
resultTo, resultWho, requestCode, startFlags, profileFile, profileFd,
null, null, options, userId);
}
可以看出,Activity的启动过程转移到了ActivityStackSupervisor的startActivityMayWait方法中了,在startActivityMayWait中有去调用startActivityLocked方法,然后startActivityLocked方法又调用startActivityUncheckedLocked方法,接着startActivityUncheckedLocked又调用了ActivitStack的resumeTopActivityLocked,这个时候启动过程已经从ActivityStackSupervisor转移到了ActivitStack。
(5)ActivitStack的resumeTopActivityLocked方法如下:
final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) { if (mStackSupervisor.inResumeTopActivity) { // Don't even start recursing. return false; } boolean result = false; try { // Protect against recursion. mStackSupervisor.inResumeTopActivity = true; if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) { mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN; mService.updateSleepIfNeededLocked(); } result = resumeTopActivityInnerLocked(prev, options); } finally { mStackSupervisor.inResumeTopActivity = false; } return result; }
可以看出来resumeTopActivityLocked调用了resumeTopActivityInnerLocked方法,而resumeTopActivityInnerLocked方法又调用了ActivityStackSupervisor的startSpecificActivityLocked方法。
startSpecificActivityLocked的源码如下:
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.task.stack.setLaunchTime(r); if (app != null && app.thread != null) { try { if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0 || !"android".equals(r.info.packageName)) { // Don't add this if it is a platform component that is marked // to run in multiple processes, because this is actually // part of the framework so doesn't make sense to track as a // separate apk in the process. 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); } // If a dead object exception was thrown -- fall through to // restart the application. } mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0, "activity", r.intent.getComponent(), false, false, true); }
可以看出来startSpecificActivityLocked方法调用了realStartActivityLocked方法。
至此,我们的调用先暂停一下,我们用一张图感观的看一下刚刚的多层调用:
(6)在ActivityStackSupervisor的realStartActivityLocked方法中有如下一段代码:
/*
* 这个app.thread的类型为IApplicationThread,
* IApplicationThread继承了IInterface接口,所以它是一个Binder类型的接口。
* 从IApplicationThread声明的接口方法可以看出,它的内部包含了大量启动、停止Activity的接口,
* 此外还包含了启动和停止服务的接口, 从接口方法的命名可以知道,
* IApplicationThread这个Binder接口的实现者完成了大量和Activity以及Service启动和停止相关的功能。
* 而IApplicationThread的实现者就是ActivityThread中的内部类ApplicationThread。
* 所以,绕来绕去,是用ApplicationThread中的scheduleLaunchActivity来启动Activity的。
*/
app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
System.identityHashCode(r), r.info, new Configuration(
mService.mConfiguration), r.compat,
app.repProcState, r.icicle, results, newIntents,
!andResume, mService.isNextTransitionForward(),
profileFile, profileFd, profileAutoStop);
(7)app.thread的类型为IApplicationThread,IApplicationThread继承了IInterface接口,所以它是一个Binder类型的接口。从IApplicationThread声明的接口方法可以看出,它的内部包含了大量启动、停止Activity的接口,此外还包含了启动和停止服务的接口, 从接口方法的命名可以知道,IApplicationThread这个Binder接口的实现者完成了大量和Activity以及Service启动和停止相关的功能。而IApplicationThread的实现者就是ActivityThread中的内部类ApplicationThread。所以,绕来绕去,是用ApplicationThread中的scheduleLaunchActivity来启动Activity的。
private class ApplicationThread extends ApplicationThreadNative
public abstract class ApplicationThreadNative extends Binder implements IApplicationThread
public interface IApplicationThread extends IInterface {
void schedulePauseActivity(IBinder token, boolean finished, boolean userLeaving,
int configChanges, boolean dontReport) throws RemoteException;
void scheduleStopActivity(IBinder token, boolean showWindow,
int configChanges) throws RemoteException;
void scheduleWindowVisibility(IBinder token, boolean showWindow) throws RemoteException;
void scheduleSleeping(IBinder token, boolean sleeping) throws RemoteException;
void scheduleResumeActivity(IBinder token, int procState, boolean isForward, Bundle resumeArgs)
throws RemoteException;
void scheduleSendResult(IBinder token, List<ResultInfo> results) throws RemoteException;
void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
ActivityInfo info, Configuration curConfig, CompatibilityInfo compatInfo,
String referrer, IVoiceInteractor voiceInteractor, int procState, Bundle state,
PersistableBundle persistentState, List<ResultInfo> pendingResults,
List<ReferrerIntent> pendingNewIntents, boolean notResumed, boolean isForward,
ProfilerInfo profilerInfo) throws RemoteException;
void scheduleRelaunchActivity(IBinder token, List<ResultInfo> pendingResults,
List<ReferrerIntent> pendingNewIntents, int configChanges,
boolean notResumed, Configuration config) throws RemoteException;
void scheduleNewIntent(List<ReferrerIntent> intent, IBinder token) throws RemoteException;
void scheduleDestroyActivity(IBinder token, boolean finished,
int configChanges) throws RemoteException;
void scheduleReceiver(Intent intent, ActivityInfo info, CompatibilityInfo compatInfo,
int resultCode, String data, Bundle extras, boolean sync,
int sendingUser, int processState) throws RemoteException;
static final int BACKUP_MODE_INCREMENTAL = 0;
static final int BACKUP_MODE_FULL = 1;
static final int BACKUP_MODE_RESTORE = 2;
static final int BACKUP_MODE_RESTORE_FULL = 3;
void scheduleCreateBackupAgent(ApplicationInfo app, CompatibilityInfo compatInfo,
int backupMode) throws RemoteException;
void scheduleDestroyBackupAgent(ApplicationInfo app, CompatibilityInfo compatInfo)
throws RemoteException;
void scheduleCreateService(IBinder token, ServiceInfo info,
CompatibilityInfo compatInfo, int processState) throws RemoteException;
void scheduleBindService(IBinder token,
Intent intent, boolean rebind, int processState) throws RemoteException;
void scheduleUnbindService(IBinder token,
Intent intent) throws RemoteException;
void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
int flags, Intent args) throws RemoteException;
void scheduleStopService(IBinder token) throws RemoteException;
static final int DEBUG_OFF = 0;
static final int DEBUG_ON = 1;
static final int DEBUG_WAIT = 2;
void bindApplication(String packageName, ApplicationInfo info, List<ProviderInfo> providers,
ComponentName testName, ProfilerInfo profilerInfo, Bundle testArguments,
IInstrumentationWatcher testWatcher, IUiAutomationConnection uiAutomationConnection,
int debugMode, boolean openGlTrace, boolean restrictedBackupMode, boolean persistent,
Configuration config, CompatibilityInfo compatInfo, Map<String, IBinder> services,
Bundle coreSettings) throws RemoteException;
void scheduleExit() throws RemoteException;
void scheduleSuicide() throws RemoteException;
void scheduleConfigurationChanged(Configuration config) throws RemoteException;
void updateTimeZone() throws RemoteException;
void clearDnsCache() throws RemoteException;
void setHttpProxy(String proxy, String port, String exclList,
Uri pacFileUrl) throws RemoteException;
void processInBackground() throws RemoteException;
void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args)
throws RemoteException;
void dumpProvider(FileDescriptor fd, IBinder servicetoken, String[] args)
throws RemoteException;
void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
int resultCode, String data, Bundle extras, boolean ordered,
boolean sticky, int sendingUser, int processState) throws RemoteException;
void scheduleLowMemory() throws RemoteException;
void scheduleActivityConfigurationChanged(IBinder token) throws RemoteException;
void profilerControl(boolean start, ProfilerInfo profilerInfo, int profileType)
throws RemoteException;
void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd)
throws RemoteException;
void setSchedulingGroup(int group) throws RemoteException;
static final int PACKAGE_REMOVED = 0;
static final int EXTERNAL_STORAGE_UNAVAILABLE = 1;
void dispatchPackageBroadcast(int cmd, String[] packages) throws RemoteException;
void scheduleCrash(String msg) throws RemoteException;
void dumpActivity(FileDescriptor fd, IBinder servicetoken, String prefix, String[] args)
throws RemoteException;
void setCoreSettings(Bundle coreSettings) throws RemoteException;
void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) throws RemoteException;
void scheduleTrimMemory(int level) throws RemoteException;
void dumpMemInfo(FileDescriptor fd, Debug.MemoryInfo mem, boolean checkin, boolean dumpInfo,
boolean dumpDalvik, String[] args) throws RemoteException;
void dumpGfxInfo(FileDescriptor fd, String[] args) throws RemoteException;
void dumpDbInfo(FileDescriptor fd, String[] args) throws RemoteException;
void unstableProviderDied(IBinder provider) throws RemoteException;
void requestAssistContextExtras(IBinder activityToken, IBinder requestToken, int requestType)
throws RemoteException;
void scheduleTranslucentConversionComplete(IBinder token, boolean timeout)
throws RemoteException;
void scheduleOnNewActivityOptions(IBinder token, ActivityOptions options)
throws RemoteException;
void setProcessState(int state) throws RemoteException;
void scheduleInstallProvider(ProviderInfo provider) throws RemoteException;
void updateTimePrefs(boolean is24Hour) throws RemoteException;
void scheduleCancelVisibleBehind(IBinder token) throws RemoteException;
void scheduleBackgroundVisibleBehindChanged(IBinder token, boolean enabled) throws RemoteException;
void scheduleEnterAnimationComplete(IBinder token) throws RemoteException;
String descriptor = "android.app.IApplicationThread";
...
}
(8)ActivityThread.java文件中的ApplicationThread内部类的scheduleLaunchActivity方法:scheduleLaunchActivity的实现很简单,就是发送一个启动Activity的消息交由Handler处理。这个Handler的名字很简洁,H。
<span style="white-space:pre"> </span>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); }
private void sendMessage(int what, Object obj, int arg1, int arg2, boolean async) { if (DEBUG_MESSAGES) Slog.v( TAG, "SCHEDULE " + what + " " + mH.codeToString(what) + ": " + arg1 + " / " + obj); Message msg = Message.obtain(); msg.what = what; msg.obj = obj; msg.arg1 = arg1; msg.arg2 = arg2; if (async) { msg.setAsynchronous(true); } mH.sendMessage(msg); }
(9)接下来看一下Handler H对消息的处理:
public void handleMessage(Message msg) {
if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
switch (msg.what) {
/*
* 启动Activity:
* */
case LAUNCH_ACTIVITY: {
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
ActivityClientRecord r = (ActivityClientRecord)msg.obj;
r.packageInfo = getPackageInfoNoCheck(
r.activityInfo.applicationInfo, r.compatInfo);
handleLaunchActivity(r, null);
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
} break;
...
}
(10)handleLaunchActivity方法:
private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
// If we are getting ready to gc after going to the background, well
// we are back active so skip it.
unscheduleGcIdler();
if (r.profileFd != null) {
mProfiler.setProfiler(r.profileFile, r.profileFd);
mProfiler.startProfiling();
mProfiler.autoStopProfiler = r.autoStopProfiler;
}
// Make sure we are running with the most recent config.
handleConfigurationChanged(null, null);
if (localLOGV) Slog.v(
TAG, "Handling launch of " + r);
/*
* 启动Activity终极大Boss在此!!!!
* */
Activity a = performLaunchActivity(r, customIntent);
if (a != null) {
r.createdConfig = new Configuration(mConfiguration);
Bundle oldState = r.state;
/*
* 调用Activity的onResume方法:
* */
handleResumeActivity(r.token, false, r.isForward,
!r.activity.mFinished && !r.startsNotResumed);
if (!r.activity.mFinished && r.startsNotResumed) {
// The activity manager actually wants this one to start out
// paused, because it needs to be visible but isn't in the
// foreground. We accomplish this by going through the
// normal startup (because activities expect to go through
// onResume() the first time they run, before their window
// is displayed), and then pausing it. However, in this case
// we do -not- need to do the full pause cycle (of freezing
// and such) because the activity manager assumes it can just
// retain the current state it has.
try {
r.activity.mCalled = false;
mInstrumentation.callActivityOnPause(r.activity);
// We need to keep around the original state, in case
// we need to be created again. But we only do this
// for pre-Honeycomb apps, which always save their state
// when pausing, so we can not have them save their state
// when restarting from a paused state. For HC and later,
// we want to (and can) let the state be saved as the normal
// part of stopping the activity.
if (r.isPreHoneycomb()) {
r.state = oldState;
}
if (!r.activity.mCalled) {
throw new SuperNotCalledException(
"Activity " + r.intent.getComponent().toShortString() +
" did not call through to super.onPause()");
}
} catch (SuperNotCalledException e) {
throw e;
} catch (Exception e) {
if (!mInstrumentation.onException(r.activity, e)) {
throw new RuntimeException(
"Unable to pause activity "
+ r.intent.getComponent().toShortString()
+ ": " + e.toString(), e);
}
}
r.paused = true;
}
} else {
// If there was an error, for any reason, tell the activity
// manager to stop us.
try {
ActivityManagerNative.getDefault()
.finishActivity(r.token, Activity.RESULT_CANCELED, null);
} catch (RemoteException ex) {
// Ignore
}
}
}
(11)从上面的代码可以看出,performLaunchActivity方法最终完成了Activity对象的创建和启动过程,并且ActivityThread通过handleResumeActivity方法来调用被启动Activity的onResume这个生命周期方法,接下来主要介绍的就是performLaunchActivity这个方法了。
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) { // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")"); /* * 第一步:从ActivityClientRecord中获取待启动的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方法使用类加载器创建Activity对象。 * */ Activity activity = null; try { java.lang.ClassLoader cl = r.packageInfo.getClassLoader(); /* * 通过类加载器创建Activity的实例对象: * */ 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); } } /* * 第三步:通过LoadedApk的makeApplication方法来尝试创建Application对象, * 而且一个应用只能有一个Application对象。 * Application对象的创建也是通过Instrumentation来完成的,这个过程和Activity对象的创建一样, * 都是通过类加载器来实现的。 * Application创建完毕后,系统会通过Instrumentation的callApplicationOnCreate来调用Application的onCreate方法。 * */ try { Application app = r.packageInfo.makeApplication(false, mInstrumentation); if (localLOGV) Slog.v(TAG, "Performing launch of " + r); if (localLOGV) Slog.v( TAG, r + ": app=" + app + ", appName=" + app.getPackageName() + ", pkg=" + r.packageInfo.getPackageName() + ", comp=" + r.intent.getComponent().toShortString() + ", dir=" + r.packageInfo.getAppDir()); if (activity != null) { /* * 第四步:创建ContextImpl对象并通过Activity的attach方法来完成一些重要数据的初始化。 * 这里有一堆Activity运行过程中所依赖的上下文环境变量, * 并通过Activity的attach方法来将这些环境变量与Activity相关联: * (2)ContextImpl是一个很重要的数据结构,它是Context的具体实现, * Context中改的大部分逻辑都是由ContextImpl来完成的。 * ContextImpl是通过Activity的attach方法来和Activity建立关联的。 * (3)此外,在attach方法中Activity还会完成Window的创建并建立自己和Window的关联, * 这样当Window接收到外部输入事件后就可以将事件传递给Activity。 * */ Context appContext = createBaseContextForActivity(r, activity); CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager()); Configuration config = new Configuration(mCompatConfiguration); if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity " + r.activityInfo.name + " with config " + config); 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); if (customIntent != null) { activity.mIntent = customIntent; } r.lastNonConfigurationInstances = null; activity.mStartedActivity = false; int theme = r.activityInfo.getThemeResource(); if (theme != 0) { activity.setTheme(theme); } activity.mCalled = false; /* * 第五步:调用Activity的onCreate方法: * 到此为止,Activity也就完成了整个启动过程, * */ if (r.isPersistable()) { mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState); } else { mInstrumentation.callActivityOnCreate(activity, r.state); } if (!activity.mCalled) { throw new SuperNotCalledException( "Activity " + r.intent.getComponent().toShortString() + " did not call through to super.onCreate()"); } 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) { throw e; } catch (Exception e) { if (!mInstrumentation.onException(activity, e)) { throw new RuntimeException( "Unable to start activity " + component + ": " + e.toString(), e); } } return activity; }
补充:
1.当我们复写onSaveInstanceState时,在activity销毁的时候会将页面的信息保存在这个map中,ActivityClientRecord是记录了页面的相关信息,在源码中还有一个ActivityRecord也是记录页面信息,在类方法之间调用时这两个类会互相复制传递。他们里面有一个Bundle成员变量是存储信息的。
final ArrayMap<IBinder, ActivityClientRecord> mActivities = new ArrayMap<>();
2.setContentView的关键代码
@Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); getDelegate().onSaveInstanceState(outState); }
public AppCompatDelegate getDelegate() { if (mDelegate == null) { mDelegate = AppCompatDelegate.create(this, this); } return mDelegate; }
public static AppCompatDelegate create(Activity activity, AppCompatCallback callback) { return create(activity, activity.getWindow(), callback); }
private static AppCompatDelegate create(Context context, Window window, AppCompatCallback callback) { final int sdk = Build.VERSION.SDK_INT; if (sdk >= 23) { return new AppCompatDelegateImplV23(context, window, callback); } else if (sdk >= 14) { return new AppCompatDelegateImplV14(context, window, callback); } else if (sdk >= 11) { return new AppCompatDelegateImplV11(context, window, callback); } else { return new AppCompatDelegateImplV7(context, window, callback); } }
在AppCompatDelegateImplV7中有setContentView方法
@Override public void setContentView(int resId) { ensureSubDecor();//获取mSubDeor,是否显示通知栏,actionbar,float等信息 ViewGroup contentParent = (ViewGroup) mSubDecor.findViewById(android.R.id.content);//获取content的布局,放我们的布局 contentParent.removeAllViews(); LayoutInflater.from(mContext).inflate(resId, contentParent); mOriginalWindowCallback.onContentChanged();//window的回调 }
启动Activity的整体流程:
startActivity —— (startActivity中调用)startActivityForResult —— (startActivityForResult中调用)Instrumentation.execStartActivity ——(execStartActivity中调用)ActivityManagerNative.getDefault().startActivity也就相当于ActivityManagerService的startActivity(IPC)——(startActivity中调用)startActivityAsUser ——(startActivityAsUser中调用)ActivityStackSupervisor.startActivityMayWait —— (startActivityMayWait中调用)startActivityLocked —— (startActivityLocked 中调用)startActivityUncheckedLocked —— (startActivityUncheckedLocked 中调用)ActivityStack的resumeTopActivityLocked(此时已经从ActivityStackSupervisor转移到了ActivityStack) —— (resumeTopActivityLocked中调用)resumeTopActivityInnerLocked —— (resumeTopActivityInnerLocked中调用)ActivityStackSupervisor的startSpecificActivityLocked(此时又从ActivityStack转移到了ActivityStackSupervisor) —— (startSpecificActivityLocked中调用)realStartActivityLocked —— (realStartActivityLocked中调用)app.thread.scheduleLaunchActivity(app.thread也就是IApplicationThread)(也就是ApplicationThread的scheduleLaunchActivity)—— (scheduleLaunchActivity中调用)queueOrSendMessage —— Handler H的handleLaunchActivity —— (handleLaunchActivity 中调用)performLaunchActivity ——(execStartActivity中调用)checkStartActivityResult
声明:ActivityManagerService是Binder,ApplicationThread是Binder。
参考:http://blog.csdn.net/zizidemenghanxiao/article/details/50639025