ActivityStackSupervisor.StartActivityUncheckedLocked()函数分析

本文详细分析了Android系统中ActivityStackSupervisor的StartActivityUncheckedLocked函数,阐述了该函数的主要职责——为启动的Activity创建Task,并将其与ActivityRecord的task字段关联。内容包括函数被调用的上下文、Task的创建条件以及任务栈的管理策略。
摘要由CSDN通过智能技术生成

一、谁调用的它?

ASS.StartActivityLocked()函数调用的它。注意这里是ASS.StartActivityLocked(),而ASS.StartActivityUncheckedLocked()在函数结尾会进入AS.StartActivityLocked(),注意两者不是相同的函数。
ASS.StartActivityLocked()函数主要作用:创建ActivityRecord。

概要分析:

这个函数定义在frameworks/base/services/java/com/android/server/am/ActivityStack.java文件中:
 ......
//在该函数中,将上一步得到的Activity信息ActivityInfo aInfo传进来,并创建ActivityRecord
 final int startActivityLocked(IApplicationThread caller,
      Intent intent, String resolvedType,
      Uri[] grantedUriPermissions,
      int grantedMode, ActivityInfo aInfo, IBinder resultTo,
             String resultWho, int requestCode,
      int callingPid, int callingUid, boolean onlyIfNeeded,
      boolean componentSpecified) 
{
            int err = START_SUCCESS;

// 从传进来的参数caller得到调用者的进程信息,并保存在callerApp变量中,这里就是Launcher应用程序的进程信息了。
//其中,IApplicationThread caller是Launcher的Binder对象,ProcessRecord callerApp是要启动的程序的进程Record。
            ProcessRecord callerApp = null;
  if (caller != null)
  {
  callerApp = mService.getRecordForAppLocked(caller);
  if (callerApp != null)
  {
    callingPid = callerApp.pid;
    callingUid = callerApp.info.uid;
   } 
  else 
  {
    ......
   }
  }

  ......
//参数resultTo是Launcher这个Activity里面的一个Binder对象,通过它可以获得Launcher这个Activity的相关信息,保存在sourceRecord变量中。
说明: IBinder resultTo:Launcher这个Activity里面的一个Binder对象; 
            String resultWho:additional identifier for use of resultT。
            ActivityRecord sourceRecord :记录Launcher的Activity的ActivityRecord。
  ActivityRecord sourceRecord = null;
  ActivityRecord resultRecord = null;
  if (resultTo != null) {
   int index = indexOfTokenLocked(resultTo);
   
   ......
    
   if (index >= 0) {
    sourceRecord = (ActivityRecord)mHistory.get(index);
    if (requestCode >= 0 && !sourceRecord.finishing) {
     ......
    }
   }
//从intent获取Flags。
  int launchFlags = intent.getFlags();

  if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0
   && sourceRecord != null) {
   ......
  }
  ......

//创建即将要启动的Activity的相关信息,并保存在r变量中。
  ActivityRecord r = new ActivityRecord(mService, this, callerApp, callingUid,
   intent, resolvedType, aInfo, mService.mConfiguration,
   resultRecord, resultWho, requestCode, componentSpecified);
  ......
//接着调用startActivityUncheckedLocked函数进行下一步操作。 
  return startActivityUncheckedLocked(r, sourceRecord,
   grantedUriPermissions, grantedMode, onlyIfNeeded, true);
 }
 ......

}



二、它是用来干什么的?

作用:为新开启的Activity创建Task,并将Task保存在ActivityRecord r.task域中。


三、具体分析:


分析(1):判断是否需要创建新的Task,设置launchFlags。



    final int startActivityUncheckedLocked(ActivityRecord r,
            ActivityRecord sourceRecord, int startFlags, boolean doResume,
            Bundle options) {
        final Intent intent = r.intent;
        final int callingUid = r.launchedFromUid;
        int launchFlags = intent.getFlags();
        // We'll invoke onUserLeaving before onPause only if the launching
        // activity did not explicitly state that this is an automated launch.
//如果lauching Activity 没有明确指出这个Activity要自动启动的话,我们就要在执行onPause函数之前执行onUseLeaving函数
        mUserLeaving = (launchFlags&Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
        if (DEBUG_USER_LEAVING) Slog.v(TAG, "startActivity() => mUserLeaving=" + mUserLeaving);
        // If the caller has asked not to resume at this point, we make note
        // of this in the record so that we can skip it when trying to find
        // the top running activity.
//如果laucher 没有要求在此时立即启动该Activity,那么 r.delayedResume = true(表示可以延迟resume),当查找最上层正在运行的Activity的时候就可以跳过它了。
        if (!doResume) {
            r.delayedResume = true;
        }
        ActivityRecord notTop = (launchFlags&Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
        // If the onlyIfNeeded flag is set, then we can do this if the activity
        // being launched is the same as the one making the call... or, as
        // a special case, if we do not know the caller then we count the
        // current top activity as the caller.
//ActivityRecord checkedCaller优先等于sourceRecord,其次选择正在运行的Activity
        if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
            ActivityRecord checkedCaller = sourceRecord;
            if (checkedCaller == null) {
                checkedCaller = getFocusedStack().topRunningNonDelayedActivityLocked(notTop);
            }
            if (!checkedCaller.realActivity.equals(r.realActivity)) {
                // Caller is not the same as launcher, so always needed.
                startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
            }
        }
//下面是三种需要创建新的TASK的情况:(1)sourceRecord=null说明要启动的Activity并不是由一个程序启动的,这时候我们总是启动一个新的TASK。(forcing " +  "Intent.FLAG_ACTIVITY_NE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值