個人博客: http://zhangsunyucong.top
前言
在這篇文章中浪册,將會基于android 26源碼上分析Activity從啟動到顯示到屏幕和Decorview添加到Window中的過程。另外在本文中航棱,省略了很多內(nèi)容放祟,目的只是從源碼中找到一條啟動的線索蚂四。遲點再補充上流程圖臣咖。
從startActivity開始說起
在應(yīng)用層開發(fā)時控硼,Acitvity跳轉(zhuǎn)會寫出下面的代碼:
public static void startAtcivity(BaseActivity activity) {
if(activity != null) {
Intent intent = new Intent(activity, HomeAcivity.class);
activity.startActivity(intent);
}
}
首先看下activity的繼承關(guān)系:
第一張圖,知道activity是context的子類戳杀,第二張圖该面,我們可以知道各種activity的關(guān)系。
另外會寫一片文章信卡,介紹context隔缀。
現(xiàn)在我們進入activity#startActivity
@Override
public void startActivity(Intent intent) {
this.startActivity(intent, null);
}
@Override
public void startActivity(Intent intent, @Nullable 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);
}
}
接著調(diào)用activity#startActivityForResult
public void startActivityForResult(@RequiresPermission Intent intent, int requestCode,
@Nullable Bundle options) {
if (mParent == null) {
options = transferSpringboardActivityOptions(options);
//1、Instrumentation
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());
}
...
} else {
...
}
}
在1傍菇、中猾瘸,出現(xiàn)了Instrumentation,并調(diào)用了execStartActivity方法
進入Instrumentation#execStartActivity
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);
ActivityResult result = null;
if (am.ignoreMatchingSpecificIntents()) {
result = am.onStartActivity(intent);
}
...
}
}
}
try {
...
//1、進入ActivityManagerService中
int result = ActivityManager.getService()
.startActivity(whoThread, who.getBasePackageName(), intent,
intent.resolveTypeIfNeeded(who.getContentResolver()),
token, target != null ? target.mEmbeddedID : null,
requestCode, 0, null, options);
//2牵触、檢查創(chuàng)建actiity過程是否產(chǎn)生了異常
checkStartActivityResult(result, intent);
} catch (RemoteException e) {
throw new RuntimeException("Failure from system", e);
}
return null;
}
在1淮悼、中,出現(xiàn)了ActivityManager揽思。取到IActivityManager袜腥,這里有涉及binder機制,ActivityManager.getService()得到的就是ActivityManagerService钉汗,ActivityManagerService實現(xiàn)了IActivityManager.Stub瞧挤,而ActivityManager中有IActivityManager.Stub.asInterface的遠程調(diào)用。
ActivityManager#getService
public static IActivityManager getService() {
return IActivityManagerSingleton.get();
}
private static final Singleton<IActivityManager> IActivityManagerSingleton =
new Singleton<IActivityManager>() {
@Override
protected IActivityManager create() {
final IBinder b = ServiceManager.getService(Context.ACTIVITY_SERVICE);
final IActivityManager am = IActivityManager.Stub.asInterface(b);
return am;
}
};
在2儡湾、中特恬,Instrumentation#checkStartActivityResult方法
/** @hide */
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)
//1、出現(xiàn)沒有注冊異常
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);
}
}
這是檢查在啟動activity過程中徐钠,可能出現(xiàn)的異常癌刽。比如,啟動的Acitivity沒有在AndroidManifest.xml中注冊尝丐,會出現(xiàn)代碼中1显拜、的異常。
繼續(xù)爹袁,回到在execStartActivity的1远荠、進入ActivityManagerService#startActivity
@Override
public int startActivity(IBinder whoThread, String callingPackage,
Intent intent, String resolvedType, Bundle bOptions) {
checkCaller();
int callingUser = UserHandle.getCallingUserId();
TaskRecord tr;
IApplicationThread appThread;
synchronized (ActivityManagerService.this) {
tr = mStackSupervisor.anyTaskForIdLocked(mTaskId);
if (tr == null) {
throw new IllegalArgumentException("Unable to find task ID " + mTaskId);
}
//1、IApplicationThread
appThread = IApplicationThread.Stub.asInterface(whoThread);
if (appThread == null) {
throw new IllegalArgumentException("Bad app thread " + appThread);
}
}
//2失息、ActivityStarter
return mActivityStarter.startActivityMayWait(appThread, -1, callingPackage, intent,
resolvedType, null, null, null, null, 0, 0, null, null,
null, bOptions, false, callingUser, null, tr, "AppTaskImpl");
}
在1譬淳、處,出現(xiàn)了IApplicationThread盹兢,這里涉及到了binder機制邻梆,IApplicationThread的實現(xiàn)是在ActivityThread中的內(nèi)部類ApplicationThread
ActivityThread#ApplicationThread
private class ApplicationThread extends IApplicationThread.Stub {
...
private void updatePendingConfiguration(Configuration config) {
...
}
public final void schedulePauseActivity(IBinder token, boolean finished,
...
}
public final void scheduleStopActivity(IBinder token, boolean showWindow,
int configChanges) {
...
}
public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
...
}
...
public final void scheduleResumeActivity(IBinder token, int processState,
boolean isForward, Bundle resumeArgs) {
...
}
public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
...
}
@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) {
...
}
@Override
public final void scheduleRelaunchActivity(IBinder token,
List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
int configChanges, boolean notResumed, Configuration config,
Configuration overrideConfig, boolean preserveWindow) {
...
}
public final void scheduleNewIntent(
List<ReferrerIntent> intents, IBinder token, boolean andPause) {
...
}
public final void scheduleDestroyActivity(IBinder token, boolean finishing,
int configChanges) {
...
}
public final void scheduleReceiver(Intent intent, ActivityInfo info,
CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
boolean sync, int sendingUser, int processState) {
...
}
...
public final void scheduleCreateService(IBinder token,
ServiceInfo info, CompatibilityInfo compatInfo, int processState) {
...
}
public final void scheduleBindService(IBinder token, Intent intent,
boolean rebind, int processState) {
...
}
public final void scheduleUnbindService(IBinder token, Intent intent) {
...
}
public final void scheduleServiceArgs(IBinder token, ParceledListSlice args) {
List<ServiceStartArgs> list = args.getList();
...
}
public final void scheduleStopService(IBinder token) {
...
}
public final void bindApplication(String processName, ApplicationInfo appInfo,
List<ProviderInfo> providers, ComponentName instrumentationName,
ProfilerInfo profilerInfo, Bundle instrumentationArgs,
IInstrumentationWatcher instrumentationWatcher,
IUiAutomationConnection instrumentationUiConnection, int debugMode,
boolean enableBinderTracking, boolean trackAllocation,
boolean isRestrictedBackupMode, boolean persistent, Configuration config,
CompatibilityInfo compatInfo, Map services, Bundle coreSettings,
String buildSerial) {
...
}
...
public void scheduleConfigurationChanged(Configuration config) {
updatePendingConfiguration(config);
...
}
...
public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
int resultCode, String dataStr, Bundle extras, boolean ordered,
boolean sticky, int sendingUser, int processState) throws RemoteException {
...
}
...
@Override
public void scheduleActivityConfigurationChanged(
IBinder token, Configuration overrideConfig) {
...
}
...
}
在ApplicationThread中浦妄,有很多與Activity剂娄,service,Application生命周期有關(guān)的方法玄呛。
其中scheduleLaunchActivity()應(yīng)該就是負責Activity創(chuàng)建的阅懦。
ActivityManagerService#startActivity的2把鉴、處故黑,調(diào)用了ActivityStarter的startActivityMayWait方法庭砍,它又調(diào)用了startActivityLocked方法
1场晶、
int startActivityLocked(IApplicationThread caller, Intent intent, Intent ephemeralIntent,
...
mLastStartActivityResult = startActivity(caller, intent, ephemeralIntent, resolvedType,
aInfo, rInfo, voiceSession, voiceInteractor, resultTo, resultWho, requestCode,
callingPid, callingUid, callingPackage, realCallingPid, realCallingUid, startFlags,
options, ignoreTargetSecurity, componentSpecified, mLastStartActivityRecord,
container, inTask);
...
return mLastStartActivityResult;
}
2、
private int startActivity(IApplicationThread caller, Intent intent, Intent ephemeralIntent,
String resolvedType, ActivityInfo aInfo, ResolveInfo rInfo,
IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
IBinder resultTo, String resultWho, int requestCode, int callingPid, int callingUid,
String callingPackage, int realCallingPid, int realCallingUid, int startFlags,
ActivityOptions options, boolean ignoreTargetSecurity, boolean componentSpecified,
ActivityRecord[] outActivity, ActivityStackSupervisor.ActivityContainer container,
TaskRecord inTask) {
...
return startActivity(r, sourceRecord, voiceSession, voiceInteractor, startFlags, true,
options, inTask, outActivity);
}
3怠缸、
private int startActivity(final ActivityRecord r, ActivityRecord sourceRecord,
...
try {
mService.mWindowManager.deferSurfaceLayout();
result = startActivityUnchecked(r, sourceRecord, voiceSession, voiceInteractor,
startFlags, doResume, options, inTask, outActivity);
} finally {
...
}
...
return result;
}
在3、中揭北,調(diào)用了startActivityUnchecked方法,startActivityUnchecked又調(diào)用了ActivityStackSupervisor#resumeFocusedStackTopActivityLocked方法恨樟,
ActivityStackSupervisor#resumeFocusedStackTopActivityLocked
boolean resumeFocusedStackTopActivityLocked(
ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
if (targetStack != null && isFocusedStack(targetStack)) {
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;
}
上面方法中疚俱,接著調(diào)用ActivityStack的resumeTopActivityUncheckedLocked方法,
ActivityStack#resumeTopActivityUncheckedLocked
boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
if (mStackSupervisor.inResumeTopActivity) {
// Don't even start recursing.
return false;
}
boolean result = false;
try {
// Protect against recursion.
mStackSupervisor.inResumeTopActivity = true;
result = resumeTopActivityInnerLocked(prev, options);
} finally {
mStackSupervisor.inResumeTopActivity = false;
}
...
return result;
}
接著調(diào)用resumeTopActivityInnerLocked方法呆奕,在resumeTopActivityInnerLocked中調(diào)用ActivityStackSupervisor的startSpecificActivityLocked方法
ActivityStackSupervisor#startSpecificActivityLocked
void startSpecificActivityLocked(ActivityRecord r,
boolean andResume, boolean checkConfig) {
...
if (app != null && app.thread != null) {
try {
if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
|| !"android".equals(r.info.packageName)) {
...
}
realStartActivityLocked(r, app, andResume, checkConfig);
return;
} catch (RemoteException e) {
...
}
}
mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
"activity", r.intent.getComponent(), false, false, true);
}
方法中調(diào)用了realStartActivityLocked方法梁钾,它里面有下面的代碼:
ActivityStackSupervisor#realStartActivityLocked
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就是ApplicationThread,并調(diào)用scheduleLaunchActivity。
上面曾經(jīng)說過ApplicationThread是AcitivityThread的內(nèi)部類零酪。
進入ApplicationThread的scheduleLaunchActivity方法拇勃,它最后會發(fā)送一個消息給名為H的handler
sendMessage(H.LAUNCH_ACTIVITY, r);
H.LAUNCH_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;
調(diào)用handleLaunchActivity方法。在handleLaunchActivity主要是分別調(diào)用performLaunchActivity和handleResumeActivity方法
進入ActivityThread#performLaunchActivity
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
// System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
//1潜秋、收集創(chuàng)建Acitivity的信息
ActivityInfo aInfo = r.activityInfo;
if (r.packageInfo == null) {
r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
Context.CONTEXT_INCLUDE_CODE);
}
...
//2、創(chuàng)建Context的實現(xiàn)者ContextImpl
ContextImpl appContext = createBaseContextForActivity(r);
Activity activity = null;
try {
//3罗售、通Instrumentation創(chuàng)建activity
java.lang.ClassLoader cl = appContext.getClassLoader();
activity = mInstrumentation.newActivity(
cl, component.getClassName(), r.intent);
...
} catch (Exception e) {
...
}
try {
//4钩述、創(chuàng)建Application
Application app = r.packageInfo.makeApplication(false, mInstrumentation);
...
if (activity != null) {
...
appContext.setOuterContext(activity);
//5、調(diào)用activity的attach方法
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 (theme != 0) {
activity.setTheme(theme);
}
activity.mCalled = false;
if (r.isPersistable()) {
//6牙勘、調(diào)用Activity的OnCreate方法
mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
} else {
mInstrumentation.callActivityOnCreate(activity, r.state);
}
...
//7所禀、調(diào)用Activity的OnStart方法
if (!r.activity.mFinished) {
activity.performStart();
r.stopped = false;
}
...
}
r.paused = true;
mActivities.put(r.token, r);
} catch (SuperNotCalledException e) {
throw e;
} catch (Exception e) {
...
}
return activity;
}
在3放钦、中,會調(diào)用Instrumentation#newActivity
public Activity newActivity(ClassLoader cl, String className,
Intent intent)
throws InstantiationException, IllegalAccessException,
ClassNotFoundException {
return (Activity)cl.loadClass(className).newInstance();
}
可以看出是通過類加載器通過反射創(chuàng)建Activity實例的操禀。
在4、中斤寂,調(diào)用了LoadedApk#makeApplication方法揪惦,
app = mActivityThread.mInstrumentation.newApplication(
cl, appClass, appContext);
makeApplication方法中,和newActivity差不多器腋,也是由Instrumentation的newApplication方法,通過反射創(chuàng)建Application的
5再愈、中調(diào)用Acitivty的attach方法护戳,
Acitivty#attach
final void attach(Context context, ActivityThread aThread,
Instrumentation instr, IBinder token, int ident,
Application application, Intent intent, ActivityInfo info,
CharSequence title, Activity parent, String id,
NonConfigurationInstances lastNonConfigurationInstances,
Configuration config, String referrer, IVoiceInteractor voiceInteractor,
Window window, ActivityConfigCallback activityConfigCallback) {
attachBaseContext(context);
mFragments.attachHost(null /*parent*/);
mWindow = new PhoneWindow(this, window, activityConfigCallback);
mWindow.setWindowControllerCallback(this);
mWindow.setCallback(this);
mWindow.setOnWindowDismissedCallback(this);
mWindow.getLayoutInflater().setPrivateFactory(this);
...
mUiThread = Thread.currentThread();
...
mWindow.setWindowManager(
(WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
mToken, mComponent.flattenToString(),
(info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
if (mParent != null) {
mWindow.setContainer(mParent.getWindow());
}
mWindowManager = mWindow.getWindowManager();
mCurrentConfig = config;
mWindow.setColorMode(info.colorMode);
}
在該方法中,主要是會創(chuàng)建PhoneWindow抗悍。
在6和7中钳枕,分別調(diào)用了acitivity的生命周期方法,onCreate和onStart鱼炒。
已經(jīng)分析了在handleLaunchActivity的performLaunchActivity方法。
現(xiàn)在分析handleLaunchActivity的handleResumeActivity指蚁,在handleResumeActivity中會調(diào)用acitivity的生命周期方法onResume和將Decorview添加到Window中自晰,并在makeVisible中顯示出來。
Activity#handleResumeActivity
final void handleResumeActivity(IBinder token,
boolean clearHide, boolean isForward, boolean reallyResume, int seq, String reason) {
ActivityClientRecord r = mActivities.get(token);
if (!checkAndUpdateLifecycleSeq(seq, r, "resumeActivity")) {
return;
}
// If we are getting ready to gc after going to the background, well
// we are back active so skip it.
unscheduleGcIdler();
mSomeActivitiesChanged = true;
//1酬荞、會調(diào)用Acitvity的onResume生命周期方法
// TODO Push resumeArgs into the activity for consideration
r = performResumeActivity(token, clearHide, reason);
if (r != null) {
final Activity a = r.activity;
...
if (!willBeVisible) {
try {
willBeVisible = ActivityManager.getService().willActivityBeVisible(
a.getActivityToken());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
if (r.window == null && !a.mFinished && willBeVisible) {
r.window = r.activity.getWindow();
View decor = r.window.getDecorView();
//2、設(shè)置decor不可見
decor.setVisibility(View.INVISIBLE);
ViewManager wm = a.getWindowManager();
WindowManager.LayoutParams l = r.window.getAttributes();
a.mDecor = decor;
l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
l.softInputMode |= forwardBit;
if (r.mPreserveWindow) {
a.mWindowAdded = true;
r.mPreserveWindow = false;
// Normally the ViewRoot sets up callbacks with the Activity
// in addView->ViewRootImpl#setView. If we are instead reusing
// the decor view we have to notify the view root that the
// callbacks may have changed.
ViewRootImpl impl = decor.getViewRootImpl();
if (impl != null) {
impl.notifyChildRebuilt();
}
}
if (a.mVisibleFromClient) {
if (!a.mWindowAdded) {
a.mWindowAdded = true;
//3勤揩、將Decorviewt添加到Window
wm.addView(decor, l);
} else {
// The activity will get a callback for this {@link LayoutParams} change
// earlier. However, at that time the decor will not be set (this is set
// in this method), so no action will be taken. This call ensures the
// callback occurs with the decor set.
a.onWindowAttributesChanged(l);
}
}
// If the window has already been added, but during resume
// we started another activity, then don't yet make the
// window visible.
} else if (!willBeVisible) {
if (localLOGV) Slog.v(
TAG, "Launch " + r + " mStartedActivity set");
r.hideForNow = true;
}
// Get rid of anything left hanging around.
cleanUpPendingRemoveWindows(r, false /* force */);
// The window is now visible if it has been added, we are not
// simply finishing, and we are not starting another activity.
if (!r.activity.mFinished && willBeVisible
&& r.activity.mDecor != null && !r.hideForNow) {
if (r.newConfig != null) {
performConfigurationChangedForActivity(r, r.newConfig);
if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
+ r.activityInfo.name + " with newConfig " + r.activity.mCurrentConfig);
r.newConfig = null;
}
if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
+ isForward);
WindowManager.LayoutParams l = r.window.getAttributes();
if ((l.softInputMode
& WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
!= forwardBit) {
l.softInputMode = (l.softInputMode
& (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
| forwardBit;
if (r.activity.mVisibleFromClient) {
ViewManager wm = a.getWindowManager();
View decor = r.window.getDecorView();
wm.updateViewLayout(decor, l);
}
}
r.activity.mVisibleFromServer = true;
mNumVisibleActivities++;
if (r.activity.mVisibleFromClient) {
//4、調(diào)用makeVisible
r.activity.makeVisible();
}
}
...
} else {
...
}
}
在1凿傅、處的performResumeActivity方法中,會調(diào)用以下代碼:
r.activity.performResume();
即調(diào)用activity的onResume生命周期方法辨液。
在2、中滔迈,設(shè)置了Decorview為不可見
在3被辑、中,將Decorview添加到window中盼理,由于2中設(shè)置了Decorview為不可見,這時view還看不到奏路。
在4臊诊、中,調(diào)用Activity的makeVisible方法抓艳。
Activity#makeVisible
void makeVisible() {
if (!mWindowAdded) {
ViewManager wm = getWindowManager();
wm.addView(mDecor, getWindow().getAttributes());
mWindowAdded = true;
}
mDecor.setVisibility(View.VISIBLE);
}
上面代碼中,將Decorview設(shè)置為可見的威兜。
剩下問題
在上面過程中的哪里開始涉及視圖繪制。遲點再看椒舵。
分析追溯到Zygote中约谈。
本篇文章的排版還有點亂犁钟。
優(yōu)秀文章:http://blog.csdn.net/dd864140130/article/details/60466394