我的应用程序中有两个活动:.
它们都是默认的LAUNCH MODE,根据我的理解应该是STANDARD模式。
第1步:现在我通过
adb shell am start -n com.study.launch/.MainActivity
命令启动MainActivity,任务堆栈变成:
* [0] MainAcitivty
这是正常的,因为正如预期的那样,创建了一个新的 MainActivity 实例。
第2步:然后我使用命令
adb shell am start -n com.study.launch/.SecondActivity
启动SecondActivity,任务堆栈变为:
* [1] SecondAcitivty
* [0] MainAcitivty
这是正常的,因为正如预期的那样,创建了 SecondActivity 的新实例,并且由于 Affnity 默认为应用程序的包名称,因此它被放入同一个任务中,尽管在启动时设置了 NEW TASK FLAG。
第 3 步:然后我使用
adb shell am start -n com.study.launch/.SecondActivity
命令再次启动 SecondActivity,任务堆栈变为:
* [2] SecondAcitivty
* [1] SecondAcitivty
* [0] MainAcitivity
这是正常的,因为正如预期的那样,创建了 SecondActivity 的新实例。
第 4 步:最后,我使用命令
adb shell am start -n com.study.launch/.MainActivity
再次启动 MainActivity。结果我给了我一个警告:
Starting: Intent { cmp=com.study.launch/.MainActivity }
Warning: Activity not started, intent has been delivered to currently running top-most instance.
检查任务堆栈,仍然成立:
* [2] SecondAcitivty
* [1] SecondAcitivty
* [0] MainAcitivity
我的问题:为什么没有创建 MainActivity 的新实例?
此时转储活动时,任务堆栈为:
ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)
Display #0 (activities from top to bottom):
Stack #2970: type=standard mode=fullscreen
isSleeping=false
mBounds=Rect(0, 0 - 0, 0)
mResumedActivity: ActivityRecord{4165bf9 u0 com.study.launch/.SecondActivity t2970}
* Task{e640079 #2970 visible=true type=standard mode=fullscreen translucent=false A=10353:com.study.launch U=0 StackId=2970 sz=3}
mBounds=Rect(0, 0 - 0, 0)
mMinWidth=-1 mMinHeight=-1
userId=0 effectiveUid=u0a353 mCallingUid=2000 mUserSetupComplete=true mCallingPackage=com.android.shell mCallingFeatureId=null
affinity=10353:com.study.launch
intent={flg=0x10000000 cmp=com.study.launch/.MainActivity}
mActivityComponent=com.study.launch/.MainActivity
autoRemoveRecents=false isPersistable=true activityType=1
rootWasReset=false mNeverRelinquishIdentity=true mReuseTask=false mLockTaskAuth=LOCK_TASK_AUTH_PINNABLE
Activities=[ActivityRecord{bcbe940 u0 com.study.launch/.MainActivity t2970}, ActivityRecord{be1bac4 u0 com.study.launch/.SecondActivity t2970}, ActivityRecord{4165bf9 u0 com.study.launch/.SecondActivity t2970}]
askedCompatMode=false inRecents=true isAvailable=true
mRootProcess=ProcessRecord{f26596c 18860:com.study.launch/u0a353}
taskId=2970 stackId=2970
mHasBeenVisible=true
mResizeMode=RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION mSupportsPictureInPicture=false isResizeable=true
lastActiveTime=932630195 (inactive for 208s)
* Hist #2: ActivityRecord{4165bf9 u0 com.study.launch/.SecondActivity t2970}
packageName=com.study.launch processName=com.study.launch
launchedFromUid=2000 launchedFromPackage=com.android.shell launchedFromFeature=null userId=0
app=ProcessRecord{f26596c 18860:com.study.launch/u0a353}
Intent { flg=0x10000000 cmp=com.study.launch/.SecondActivity }
rootOfTask=false task=Task{e640079 #2970 visible=true type=standard mode=fullscreen translucent=false A=10353:com.study.launch U=0 StackId=2970 sz=3}
taskAffinity=10353:com.study.launch
mActivityComponent=com.study.launch/.SecondActivity
baseDir=/data/app/~~16sf25MRe03beHbyI0Sc3g==/com.study.launch-SSF_oypq9Ln-G52kGNzmdw==/base.apk
dataDir=/data/user/0/com.study.launch
stateNotNeeded=false componentSpecified=false mActivityType=standard
compat={560dpi} labelRes=0x7f090055 icon=0x7f080001 theme=0x7f0a000a
mLastReportedConfigurations:
mGlobalConfig={1.0 ?mcc?mnc [zh_CN] ldltr sw411dp w411dp h659dp 560dpi nrml port finger -keyb/v/h -nav/h winConfig={ mBounds=Rect(0, 0 - 1440, 2560) mAppBounds=Rect(0, 0 - 1440, 2392) mWindowingMode=fullscreen mDisplayWindowingMode=fullscreen mActivityType=undefined mAlwaysOnTop=undefined mRotation=ROTATION_0} s.61}
mOverrideConfig={1.0 ?mcc?mnc [zh_CN] ldltr sw411dp w411dp h659dp 560dpi nrml port finger -keyb/v/h -nav/h winConfig={ mBounds=Rect(0, 0 - 1440, 2560) mAppBounds=Rect(0, 0 - 1440, 2392) mWindowingMode=fullscreen mDisplayWindowingMode=fullscreen mActivityType=standard mAlwaysOnTop=undefined mRotation=ROTATION_0} s.1}
CurrentConfiguration={1.0 ?mcc?mnc [zh_CN] ldltr sw411dp w411dp h659dp 560dpi nrml port finger -keyb/v/h -nav/h winConfig={ mBounds=Rect(0, 0 - 1440, 2560) mAppBounds=Rect(0, 0 - 1440, 2392) mWindowingMode=fullscreen mDisplayWindowingMode=fullscreen mActivityType=standard mAlwaysOnTop=undefined mRotation=ROTATION_0} s.1}
taskDescription: label="null" icon=null iconResource=/0 iconFilename=null primaryColor=fff5f5f5
backgroundColor=fffafafa statusBarColor=0 navigationBarColor=0
launchFailed=false launchCount=1 lastLaunchTime=-3m28s587ms
mHaveState=false mIcicle=null
state=RESUMED stopped=false delayedResume=false finishing=false
keysPaused=false inHistory=true setToSleep=false idle=true mStartingWindowState=STARTING_WINDOW_NOT_SHOWN
occludesParent=true noDisplay=false immersive=false launchMode=0
frozenBeforeDestroy=false forceNewConfig=false
mActivityType=standard
windows=[Window{ef30aec u0 com.study.launch/com.study.launch.SecondActivity}]
windowType=2 hasVisible=true
mOccludesParent=true mOrientation=-1
mVisibleRequested=true mVisible=true mClientVisible=true reportedDrawn=true reportedVisible=true
mNumInterestingWindows=1 mNumDrawnWindows=1 allDrawn=true lastAllDrawn=true)
startingData=null firstWindowDrawn=true mIsExiting=false
nowVisible=true lastVisibleTime=-3m28s54ms
resizeMode=RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION
mLastReportedMultiWindowMode=false mLastReportedPictureInPictureMode=false
* Hist #1: ActivityRecord{be1bac4 u0 com.study.launch/.SecondActivity t2970}
packageName=com.study.launch processName=com.study.launch
launchedFromUid=2000 launchedFromPackage=com.android.shell launchedFromFeature=null userId=0
app=ProcessRecord{f26596c 18860:com.study.launch/u0a353}
Intent { flg=0x10000000 cmp=com.study.launch/.SecondActivity }
rootOfTask=false task=Task{e640079 #2970 visible=true type=standard mode=fullscreen translucent=false A=10353:com.study.launch U=0 StackId=2970 sz=3}
taskAffinity=10353:com.study.launch
mActivityComponent=com.study.launch/.SecondActivity
baseDir=/data/app/~~16sf25MRe03beHbyI0Sc3g==/com.study.launch-SSF_oypq9Ln-G52kGNzmdw==/base.apk
dataDir=/data/user/0/com.study.launch
stateNotNeeded=false componentSpecified=false mActivityType=standard
compat={560dpi} labelRes=0x7f090055 icon=0x7f080001 theme=0x7f0a000a
mLastReportedConfigurations:
mGlobalConfig={1.0 ?mcc?mnc [zh_CN] ldltr sw411dp w411dp h659dp 560dpi nrml port finger -keyb/v/h -nav/h winConfig={ mBounds=Rect(0, 0 - 1440, 2560) mAppBounds=Rect(0, 0 - 1440, 2392) mWindowingMode=fullscreen mDisplayWindowingMode=fullscreen mActivityType=undefined mAlwaysOnTop=undefined mRotation=ROTATION_0} s.61}
mOverrideConfig={1.0 ?mcc?mnc [zh_CN] ldltr sw411dp w411dp h659dp 560dpi nrml port finger -keyb/v/h -nav/h winConfig={ mBounds=Rect(0, 0 - 1440, 2560) mAppBounds=Rect(0, 0 - 1440, 2392) mWindowingMode=fullscreen mDisplayWindowingMode=fullscreen mActivityType=standard mAlwaysOnTop=undefined mRotation=ROTATION_0} s.1}
CurrentConfiguration={1.0 ?mcc?mnc [zh_CN] ldltr sw411dp w411dp h659dp 560dpi nrml port finger -keyb/v/h -nav/h winConfig={ mBounds=Rect(0, 0 - 1440, 2560) mAppBounds=Rect(0, 0 - 1440, 2392) mWindowingMode=fullscreen mDisplayWindowingMode=fullscreen mActivityType=standard mAlwaysOnTop=undefined mRotation=ROTATION_0} s.1}
taskDescription: label="null" icon=null iconResource=/0 iconFilename=null primaryColor=fff5f5f5
backgroundColor=fffafafa statusBarColor=0 navigationBarColor=0
launchFailed=false launchCount=0 lastLaunchTime=-4m48s99ms
mHaveState=true mIcicle=Bundle[mParcelledData.dataSize=1380]
state=STOPPED stopped=true delayedResume=false finishing=false
keysPaused=false inHistory=true setToSleep=false idle=true mStartingWindowState=STARTING_WINDOW_NOT_SHOWN
occludesParent=true noDisplay=false immersive=false launchMode=0
frozenBeforeDestroy=false forceNewConfig=false
mActivityType=standard
windows=[Window{4792473 u0 com.study.launch/com.study.launch.SecondActivity}]
windowType=2 hasVisible=true
mOccludesParent=true mOrientation=-1
mVisibleRequested=false mVisible=false mClientVisible=false reportedDrawn=false reportedVisible=false
mAppStopped=true
mNumInterestingWindows=1 mNumDrawnWindows=1 allDrawn=true lastAllDrawn=true)
startingData=null firstWindowDrawn=true mIsExiting=false
nowVisible=false lastVisibleTime=-4m47s573ms
resizeMode=RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION
mLastReportedMultiWindowMode=false mLastReportedPictureInPictureMode=false
* Hist #0: ActivityRecord{bcbe940 u0 com.study.launch/.MainActivity t2970}
packageName=com.study.launch processName=com.study.launch
launchedFromUid=2000 launchedFromPackage=com.android.shell launchedFromFeature=null userId=0
app=ProcessRecord{f26596c 18860:com.study.launch/u0a353}
Intent { flg=0x10000000 cmp=com.study.launch/.MainActivity }
rootOfTask=true task=Task{e640079 #2970 visible=true type=standard mode=fullscreen translucent=false A=10353:com.study.launch U=0 StackId=2970 sz=3}
taskAffinity=10353:com.study.launch
mActivityComponent=com.study.launch/.MainActivity
baseDir=/data/app/~~16sf25MRe03beHbyI0Sc3g==/com.study.launch-SSF_oypq9Ln-G52kGNzmdw==/base.apk
dataDir=/data/user/0/com.study.launch
stateNotNeeded=false componentSpecified=false mActivityType=standard
compat={560dpi} labelRes=0x7f090001 icon=0x7f080001 theme=0x7f0a000a
mLastReportedConfigurations:
mGlobalConfig={1.0 ?mcc?mnc [zh_CN] ldltr sw411dp w411dp h659dp 560dpi nrml port finger -keyb/v/h -nav/h winConfig={ mBounds=Rect(0, 0 - 1440, 2560) mAppBounds=Rect(0, 0 - 1440, 2392) mWindowingMode=fullscreen mDisplayWindowingMode=fullscreen mActivityType=undefined mAlwaysOnTop=undefined mRotation=ROTATION_0} s.61}
mOverrideConfig={1.0 ?mcc?mnc [zh_CN] ldltr sw411dp w411dp h659dp 560dpi nrml port finger -keyb/v/h -nav/h winConfig={ mBounds=Rect(0, 0 - 1440, 2560) mAppBounds=Rect(0, 0 - 1440, 2392) mWindowingMode=fullscreen mDisplayWindowingMode=fullscreen mActivityType=standard mAlwaysOnTop=undefined mRotation=ROTATION_0} s.1}
CurrentConfiguration={1.0 ?mcc?mnc [zh_CN] ldltr sw411dp w411dp h659dp 560dpi nrml port finger -keyb/v/h -nav/h winConfig={ mBounds=Rect(0, 0 - 1440, 2560) mAppBounds=Rect(0, 0 - 1440, 2392) mWindowingMode=fullscreen mDisplayWindowingMode=fullscreen mActivityType=standard mAlwaysOnTop=undefined mRotation=ROTATION_0} s.1}
taskDescription: label="null" icon=null iconResource=/0 iconFilename=null primaryColor=fff5f5f5
backgroundColor=fffafafa statusBarColor=0 navigationBarColor=0
launchFailed=false launchCount=0 lastLaunchTime=-5m43s524ms
mHaveState=true mIcicle=Bundle[mParcelledData.dataSize=1380]
state=STOPPED stopped=true delayedResume=false finishing=false
keysPaused=false inHistory=true setToSleep=false idle=true mStartingWindowState=STARTING_WINDOW_REMOVED
occludesParent=true noDisplay=false immersive=false launchMode=0
frozenBeforeDestroy=false forceNewConfig=false
mActivityType=standard
windows=[Window{76c0bff u0 com.study.launch/com.study.launch.MainActivity}]
windowType=2 hasVisible=true
mOccludesParent=true mOrientation=-1
mVisibleRequested=false mVisible=false mClientVisible=false reportedDrawn=false reportedVisible=false
mAppStopped=true
mNumInterestingWindows=1 mNumDrawnWindows=1 allDrawn=true lastAllDrawn=true)
startingData=null firstWindowDrawn=true mIsExiting=false
nowVisible=false lastVisibleTime=-5m41s774ms
resizeMode=RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION
mLastReportedMultiWindowMode=false mLastReportedPictureInPictureMode=false
另外,我发现如果应用程序的第一次启动(进程启动)是通过 adb shell am start 启动的,那么问题可以重现。
如果应用程序的首次启动是在启动器中单击启动,则该问题不再重现。
当您启动
Activity
时,Android 会查看任务根部是否已存在具有该 Activity
的任务(它还会比较一些 Intent
标志,在您的情况下这些标志也会匹配)。如果它找到匹配项,它只是假设您正在尝试将该任务带到前台,并且它会这样做:将现有任务带到前台而不启动 Activity
的实例。
这就是为什么当您在后台有一个任务时,当您点击主屏幕上的应用程序图标时,现有任务将被带到前台,而不是 Android 再次启动第一个 Activity
。这也是当您从“最近任务列表”中选择某项内容时 Android 将另一个任务带到前台的方式。它只是再次启动 root Activity
,这会导致 Android 找到现有任务并将其带到前台。
现在通常,当启动器(主屏幕)启动应用程序时,它会在
Intent
中设置一些附加参数:
FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
ACTION_MAIN
CATEGORY_LAUNCHER
这样,当您从主屏幕再次启动应用程序时,
Intent
也会设置这些值,以便它们与原始 Intent
中的值相匹配。由于您使用 ADB 启动活动,因此不会设置这些标志。这与现实场景中发生的情况并不完全匹配,这就是为什么您会看到一些差异。希望这有帮助!