终结篇:Android startActivity原理分析(基于Android 8

    // We are starting up the next activity, so tell the window manager

    // that the previous one will be hidden soon.  This way it can know

    // to ignore it when computing the desired screen orientation.

    //这里准备开启页面

    boolean anim = true;

    if (prev != null) {

        if (prev.finishing) {

            if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,

                    "Prepare close transition: prev=" + prev);

            if (mNoAnimActivities.contains(prev)) {

                anim = false;

                mWindowManager.prepareAppTransition(TRANSIT_NONE, false);

            } else {

                mWindowManager.prepareAppTransition(prev.getTask() == next.getTask()

                        ? TRANSIT_ACTIVITY_CLOSE

                        : TRANSIT_TASK_CLOSE, false);

            }

            prev.setVisibility(false);

        } else {

            if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,

                    "Prepare open transition: prev=" + prev);

            if (mNoAnimActivities.contains(next)) {

                anim = false;

                mWindowManager.prepareAppTransition(TRANSIT_NONE, false);

            } else {

                //准备启动

                mWindowManager.prepareAppTransition(prev.getTask() == next.getTask()

                        ? TRANSIT_ACTIVITY_OPEN

                        : next.mLaunchTaskBehind

                                ? TRANSIT_TASK_OPEN_BEHIND

                                : TRANSIT_TASK_OPEN, false);

            }

        }

    } else {

        if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");

        if (mNoAnimActivities.contains(next)) {

            anim = false;

            mWindowManager.prepareAppTransition(TRANSIT_NONE, false);

        } else {

            mWindowManager.prepareAppTransition(TRANSIT_ACTIVITY_OPEN, false);

        }

    }



    Bundle resumeAnimOptions = null;

    if (anim) {

        ActivityOptions opts = next.getOptionsForTargetActivityLocked();

        if (opts != null) {

            resumeAnimOptions = opts.toBundle();

        }

        next.applyOptionsLocked();

    } else {

        next.clearOptionsLocked();

    }



    ActivityStack lastStack = mStackSupervisor.getLastStack();

    if (next.app != null && next.app.thread != null) {

        if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next

                + " stopped=" + next.stopped + " visible=" + next.visible);



        // If the previous activity is translucent, force a visibility update of

        // the next activity, so that it's added to WM's opening app list, and

        // transition animation can be set up properly.

        // For example, pressing Home button with a translucent activity in focus.

        // Launcher is already visible in this case. If we don't add it to opening

        // apps, maybeUpdateTransitToWallpaper() will fail to identify this as a

        // TRANSIT_WALLPAPER_OPEN animation, and run some funny animation.

        final boolean lastActivityTranslucent = lastStack != null

                && (!lastStack.mFullscreen

                || (lastStack.mLastPausedActivity != null

                && !lastStack.mLastPausedActivity.fullscreen));

        // The contained logic must be synchronized, since we are both changing the visibility

        // and updating the {@link Configuration}. {@link ActivityRecord#setVisibility} will

        // ultimately cause the client code to schedule a layout. Since layouts retrieve the

        // current {@link Configuration}, we must ensure that the below code updates it before

        // the layout can occur.

        synchronized(mWindowManager.getWindowManagerLock()) {

            // This activity is now becoming visible.

            if (!next.visible || next.stopped || lastActivityTranslucent) {

                next.setVisibility(true);

            }



            // schedule launch ticks to collect information about slow apps.

            next.startLaunchTickingLocked();



            ActivityRecord lastResumedActivity =

                    lastStack == null ? null :lastStack.mResumedActivity;

            ActivityState lastState = next.state;



            mService.updateCpuStats();



            if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next

                    + " (in existing)");



            setResumedActivityLocked(next, "resumeTopActivityInnerLocked");

            //更新进程信息

            mService.updateLruProcessLocked(next.app, true, null);

            updateLRUListLocked(next);

            mService.updateOomAdjLocked();



            // Have the window manager re-evaluate the orientation of

            // the screen based on the new activity order.

            boolean notUpdated = true;



            if (mStackSupervisor.isFocusedStack(this)) {



                // We have special rotation behavior when Keyguard is locked. Make sure all

                // activity visibilities are set correctly as well as the transition is updated

                // if needed to get the correct rotation behavior.

                // TODO: Remove this once visibilities are set correctly immediately when

                // starting an activity.

                if (mStackSupervisor.mKeyguardController.isKeyguardLocked()) {

                    mStackSupervisor.ensureActivitiesVisibleLocked(null /* starting */,

                            0 /* configChanges */, false /* preserveWindows */);

                }

                final Configuration config = mWindowManager.updateOrientationFromAppTokens(

                        mStackSupervisor.getDisplayOverrideConfiguration(mDisplayId),

                        next.mayFreezeScreenLocked(next.app) ? next.appToken : null,

                                mDisplayId);

                if (config != null) {

                    next.frozenBeforeDestroy = true;

                }

                notUpdated = !mService.updateDisplayOverrideConfigurationLocked(config, next,

                        false /* deferResume */, mDisplayId);

            }



            if (notUpdated) {

                // The configuration update wasn't able to keep the existing

                // instance of the activity, and instead started a new one.

                // We should be all done, but let's just make sure our activity

                // is still at the top and schedule another run if something

                // weird happened.

                ActivityRecord nextNext = topRunningActivityLocked();

                if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,

                        "Activity config changed during resume: " + next

                                + ", new next: " + nextNext);

                if (nextNext != next) {

                    // Do over!

                    mStackSupervisor.scheduleResumeTopActivities();

                }

                if (!next.visible || next.stopped) {

                    next.setVisibility(true);

                }

                next.completeResumeLocked();

                if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();

                return true;

            }



            try {

                // 判断是否需要触发onActivityResult函数

                ArrayList<ResultInfo> a = next.results;

                if (a != null) {

                    final int N = a.size();

                    if (!next.finishing && N > 0) {

                        if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,

                                "Delivering results to " + next + ": " + a);

                        next.app.thread.scheduleSendResult(next.appToken, a);

                    }

                }



                //是否需要触发onNewIntent函数

                if (next.newIntents != null) {

                    next.app.thread.scheduleNewIntent(

                            next.newIntents, next.appToken, false /* andPause */);

                }



                // Well the app will no longer be stopped.

                // Clear app token stopped state in window manager if needed.

                next.notifyAppResumed(next.stopped);



                EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,

                        System.identityHashCode(next), next.getTask().taskId,

                        next.shortComponentName);



                next.sleeping = false;

                mService.showUnsupportedZoomDialogIfNeededLocked(next);

                mService.showAskCompatModeDialogLocked(next);

                next.app.pendingUiClean = true;

                next.app.forceProcessStateUpTo(mService.mTopProcessState);

                next.clearOptionsLocked();

                //最后通过ActivityThread的scheduleResumeActivity函数进行启动

                next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,

                        mService.isNextTransitionForward(), resumeAnimOptions);



                if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed "

                        + next);

            } catch (Exception e) {

                // Whoops, need to restart this activity!

                if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "

                        + lastState + ": " + next);

                next.state = lastState;

                if (lastStack != null) {

                    lastStack.mResumedActivity = lastResumedActivity;

                }

                Slog.i(TAG, "Restarting because process died: " + next);

                if (!next.hasBeenLaunched) {

                    next.hasBeenLaunched = true;

                } else  if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&

                        mStackSupervisor.isFrontStackOnDisplay(lastStack)) {

                    next.showStartingWindow(null /* prev */, false /* newTask */,

                            false /* taskSwitch */);

                }

                mStackSupervisor.startSpecificActivityLocked(next, true, false);

                if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();

                return true;

            }

        }



        // From this point on, if something goes wrong there is no way

        // to recover the activity.

        try {

            next.completeResumeLocked();

        } catch (Exception e) {

            // If any exception gets thrown, toss away this

            // activity and try the next one.

            Slog.w(TAG, "Exception thrown during resume of " + next, e);

            requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,

                    "resume-exception", true);

            if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();

            return true;

        }

    } else {

        // Whoops, need to restart this activity!

        if (!next.hasBeenLaunched) {

            next.hasBeenLaunched = true;

        } else {

            if (SHOW_APP_STARTING_PREVIEW) {

                next.showStartingWindow(null /* prev */, false /* newTask */,

                        false /* taskSwich */);

            }

            if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);

        }

        if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);

        mStackSupervisor.startSpecificActivityLocked(next, true, true);

    }



    if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();

    return true;

}



这个函数就是对目标页的很多状态判断,还有pause我们的启动者,然后通知AT(ActivityThread)准备启动,我们继续查看AT内的启动逻辑



@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);

    }



这个函数内创建了一个ActivityClientRecord对象,然后通过handler发送命令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);

}




这个case下调用了handleLaunchActivity函数,我们继续跟进分析



private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent, String reason) {

    // If we are getting ready to gc after going to the background, well

    // we are back active so skip it.

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
img

实战系列

话不多说,Android实战系列集合都已经系统分类好,由于文章篇幅问题没法过多展示


一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
img
U-1712755797731)]
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
[外链图片转存中…(img-4Iz4IlTL-1712755797732)]

实战系列

话不多说,Android实战系列集合都已经系统分类好,由于文章篇幅问题没法过多展示

[外链图片转存中…(img-yR7BXKP1-1712755797732)]
[外链图片转存中…(img-U2jWGiFO-1712755797733)]

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
[外链图片转存中…(img-pJ5FMM6A-1712755797733)]

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值