androidandroid-activitylaunchmode

Why is my standard activity not newly instantiated in Android (result START_DELIVERED_TO_TOP)?


There are two Activities in my app:.

They are both in default LAUNCH MODE, which according to my understanding should be STANDARD mode.

Step 1: Now I start MainActivity by adb shell am start -n com.study.launch/.MainActivity command and the task stack becomes:

* [0] MainAcitivty

This is normal, because as expected, a new instance of MainActivity is created.

Step 2: Then I start SecondActivity with the command adb shell am start -n com.study.launch/.SecondActivity and the task stack becomes:

* [1] SecondAcitivty
* [0] MainAcitivty

This is normal, because as expected, a new instance of SecondActivity is created, and because Affnity defaults to the package name of the app, it is put into the same task, although the NEW TASK FLAG is set when am start.

Step 3: I then start SecondActivity again with the adb shell am start -n com.study.launch/.SecondActivity command and the task stack becomes:

* [2] SecondAcitivty
* [1] SecondAcitivty
* [0] MainAcitivity

This is normal, because as expected, a new instance of SecondActivity is created.

Step 4: Finally, I start MainActivity again with the command adb shell am start -n com.study.launch/.MainActivity. am gives me a warning as a result:

Starting: Intent { cmp=com.study.launch/.MainActivity }
Warning: Activity not started, intent has been delivered to currently running top-most instance.

Checking the task stack, it still holds:

* [2] SecondAcitivty
* [1] SecondAcitivty
* [0] MainAcitivity

My question: why is no new instance of MainActivity created?

At this point when dump activities, the task stack is:

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

Also, I found that if the first start of the app (process start) is started by adb shell am start, then the problem can be reproduced.

If the first start of the app is a click start in the launcher, then the issue no longer reproduces.


Solution

  • When you launch an Activity, Android looks to see if there is already a task that has that Activity at the root of the task (it also compares some Intent flags, which in your case would also match). If it finds a match, it just assumes that you are trying to bring that task to the foreground, and it does just that: brings the existing task to the foreground without launching an instance of the Activity. This is why, when you have a task in the background, and you tap the app icon on the HOME screen, the existing task is brought to the foreground instead of Android launching the first Activity again. This is also how Android brings another task to the foreground when you select something from the "recent task list". It just launches the root Activity again, which causes Android to find the existing task and bring it to the foreground.

    Now usually, when the launcher (HOME screen) launches an app, it sets a few additional parameters in the Intent:

    so that when you launch the app again from the HOME screen, the Intent also has these values set so they will match the values from the original Intent. Since you are using ADB to launch activities, these flags won't be set. This doesn't exactly match what is going on in a real-world scenario, which is why you see some differences. Hope this helps!