-
r.autoStopProfiler = autoStopProfiler;
-
updatePendingConfiguration(curConfig);
-
queueOrSendMessage(H.LAUNCH_ACTIVITY, r);
-
}
说明:上述代码很好理解,构造一个activity记录,然后发送一个消息,所以,我们要看看Handler是如何处理这个消息的,现在转到这个Handler,它有个很短的名字叫做H
code:ActivityThread#H
[java] view plain copy
-
//这个类太长,我只帖出了我们用到的部分
-
private class H extends Handler {
-
public void handleMessage(Message msg) {
-
if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
-
switch (msg.what) {
-
//这里处理LAUNCH_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);
-
//这里处理startActivity消息
-
handleLaunchActivity(r, null);
-
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
-
} break;
-
case RELAUNCH_ACTIVITY: {
-
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, “activityRestart”);
-
ActivityClientRecord r = (ActivityClientRecord)msg.obj;
-
handleRelaunchActivity®;
-
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
-
} break;
-
case PAUSE_ACTIVITY:
-
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, “activityPause”);
-
handlePauseActivity((IBinder)msg.obj, false, msg.arg1 != 0, msg.arg2);
-
maybeSnapshot();
-
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
-
break;
-
…
-
}
-
}
说明:看来还要看handleLaunchActivity
code:ActivityThread#handleLaunchActivity
[java] view plain copy
-
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);
-
//终于到底了,大家都有点不耐烦了吧,从方法名可以看出,
-
//performLaunchActivity真正完成了activity的调起,
-
//同时activity会被实例化,并且onCreate会被调用
-
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;
-
//同时,由于新activity被调起了,原activity的onPause会被调用
-
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
-
}
-
}
-
}
说明:关于原activity和新activity之间的状态同步,如果大家感兴趣可以自己研究下,因为逻辑太复杂,我没法把所有问题都说清楚,否则就太深入细节而淹没了整体逻辑,研究源码要的就是清楚整体逻辑。下面看最后一个方法,这个方法是activity的启动过程的真正实现。
code:ActivityThread#performLaunchActivity
[java] view plain copy
-
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
-
// System.out.println(“##### [” + System.currentTimeMillis() + “] ActivityThread.performLaunchActivity(” + r + “)”);
-
ActivityInfo aInfo = r.activityInfo;
-
if (r.packageInfo == null) {
-
r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
-
Context.CONTEXT_INCLUDE_CODE);
-
}
-
//首先从intent中解析出目标activity的启动参数
-
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);
-
}
-
Activity activity = null;
-
try {
-
java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
-
//用ClassLoader(类加载器)将目标activity的类通过类名加载进来并调用newInstance来实例化一个对象
-
//其实就是通过Activity的无参构造方法来new一个对象,对象就是在这里new出来的。
-
activity = mInstrumentation.newActivity(
-
cl, component.getClassName(), r.intent);
-
StrictMode.incrementExpectedActivityCount(activity.getClass());
-
r.intent.setExtrasClassLoader(cl);
-
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);
-
}
-
}
-
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()
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
最后是今天给大家分享的一些独家干货:
【Android开发核心知识点笔记】
【Android思维脑图(技能树)】
【Android核心高级技术PDF文档,BAT大厂面试真题解析】
【Android高级架构视频学习资源】
一个人可以走的很快,但一群人才能走的更远。如果你从事以下工作或对以下感兴趣,欢迎戳这里加入程序员的圈子,让我们一起学习成长!
AI人工智能、Android移动开发、AIGC大模型、C C#、Go语言、Java、Linux运维、云计算、MySQL、PMP、网络安全、Python爬虫、UE5、UI设计、Unity3D、Web前端开发、产品经理、车载开发、大数据、鸿蒙、计算机网络、嵌入式物联网、软件测试、数据结构与算法、音视频开发、Flutter、IOS开发、PHP开发、.NET、安卓逆向、云计算
【Android高级架构视频学习资源】*
一个人可以走的很快,但一群人才能走的更远。如果你从事以下工作或对以下感兴趣,欢迎戳这里加入程序员的圈子,让我们一起学习成长!
AI人工智能、Android移动开发、AIGC大模型、C C#、Go语言、Java、Linux运维、云计算、MySQL、PMP、网络安全、Python爬虫、UE5、UI设计、Unity3D、Web前端开发、产品经理、车载开发、大数据、鸿蒙、计算机网络、嵌入式物联网、软件测试、数据结构与算法、音视频开发、Flutter、IOS开发、PHP开发、.NET、安卓逆向、云计算