Android源码解析系列1——Activity启动和界面加载,安卓游戏开发

在scheduleLaunchActivity方法中,首先创建了一个ActivityClientRecord对象,并对其进行赋值。

ActivityClientRecord:用于记录与Activity相关的数据。

然后通过Handler,将线程由Binder线程切换到主线程。最终调用到ActivityThread的handleLaunchActivity方法。

public final class ActivityThread {
// 省略部分代码

private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent, String reason) {
// 省略部分代码

Activity a = performLaunchActivity(r, customIntent);

if (a != null) {
// 省略部分代码
handleResumeActivity(r.token, false, r.isForward,
!r.activity.mFinished && !r.startsNotResumed, r.lastProcessedSeq, reason);

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.
performPauseActivityIfNeeded(r, reason);
// 省略部分代码
}
}
}
}

这里,我们暂时不管performLaunchActivity方法中做了什么,仅分析后续代码。后续代码中,调用了handleResumeActivity,猜测它应该会调用Activity的onResume方法。

根据Activity生命周期推测到:

performLaunchActivity方法里,一定会依次调用Activity的onCreateonStart方法。

带着这个思路,开始分析performLaunchActivity方法。

public final class ActivityThread {
// 省略部分代码

private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
// 省略部分代码

Activity activity = null;
try {
java.lang.ClassLoader cl = appContext.getClassLoader();
activity = mInstrumentation.newActivity(
cl, component.getClassName(), r.intent);
// 省略部分代码
} catch (Exception e) { /* 省略部分代码 */ }

try {
Application app = r.packageInfo.makeApplication(false, mInstrumentation);
// 省略部分代码

if (activity != null) {
// 省略部分代码

Window window = null;
if (r.mPendingRemoveWindow != null && r.mPreserveWindow) {
window = r.mPendingRemoveWindow;
r.mPendingRemoveWindow = null;
r.mPendingRemoveWindowManager = null;
}
appContext.setOuterContext(activity);
activity.attach(appContext, this, getInstrumentation(), r.token,
r.ident, app, r.intent, r.activityInfo, title, r.pare

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值