Start activity with flag FLAG_ACTIVITY_NEW_TASK, the new activity didn't in new task

2

First I start Activity with FLAG_ACTIVITY_NEW_TASK,then using command 'adb shell dumpsys Activity',finally i find the two activities in one task。

 Intent intent = new Intent(this, Main2Activity.class);
 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 startActivity(intent);

These Activity were normal, i didn't modify them。These were stack information.

ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)
Display #0 (activities from top to bottom):
  Stack #1:
  mFullscreen=true
  mBounds=null
    Task id #2493
    mFullscreen=true
    mBounds=null
    mMinWidth=-1
    mMinHeight=-1
    mLastNonFullscreenBounds=null
    * TaskRecord{cb64df8 #2493 A=com.example.flag U=0 StackId=1 sz=2}
      userId=0 effectiveUid=u0a63 mCallingUid=2000 mUserSetupComplete=true mCallingPackage=null
      affinity=com.example.flag
      intent={act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.flag/.MainActivity}
      realActivity=com.example.flag/.MainActivity
      autoRemoveRecents=false isPersistable=true numFullscreen=2 taskType=0 mTaskToReturnTo=1
      rootWasReset=false mNeverRelinquishIdentity=true mReuseTask=false mLockTaskAuth=LOCK_TASK_AUTH_PINNABLE
      Activities=[ActivityRecord{b786d24 u0 com.example.flag/.MainActivity t2493}, ActivityRecord{afe6f61 u0 com.example.flag/.Main2Activity t2493}]
      askedCompatMode=false inRecents=true isAvailable=true
      lastThumbnail=null lastThumbnailFile=/data/system_ce/0/recent_images/2493_task_thumbnail.png
      stackId=1
      hasBeenVisible=true mResizeMode=RESIZE_MODE_RESIZEABLE isResizeable=true firstActiveTime=1499670460422 lastActiveTime=1499670460422 (inactive for 23s)
      * Hist #1: ActivityRecord{afe6f61 u0 com.example.flag/.Main2Activity t2493}
          packageName=com.example.flag processName=com.example.flag
          launchedFromUid=10063 launchedFromPackage=com.example.flag userId=0
          app=ProcessRecord{b30f2d1 11785:com.example.flag/u0a63}
          Intent { flg=0x10000000 cmp=com.example.flag/.Main2Activity }
          frontOfTask=false task=TaskRecord{cb64df8 #2493 A=com.example.flag U=0 StackId=1 sz=2}
          taskAffinity=com.example.flag
          realActivity=com.example.flag/.Main2Activity
          baseDir=/data/app/com.example.flag-1/base.apk
          dataDir=/data/user/0/com.example.flag
          stateNotNeeded=false componentSpecified=true mActivityType=0
          compat={480dpi} labelRes=0x7f060021 icon=0x7f030000 theme=0x7f0800a3
          config={1.0 460mcc1mnc [zh_CN_#Hans,en_US,zh_TW_#Hant] ldltr sw360dp w360dp h616dp 480dpi nrml long port finger -keyb/v/h -nav/h s.8}
          taskConfigOverride={1.0 ?mcc?mnc ?localeList ?layoutDir ?swdp ?wdp ?hdp ?density ?lsize ?long ?orien ?uimode ?night ?touch ?keyb/?/? ?nav/?}
          taskDescription: iconFilename=null label="null" color=ff3f51b5
          launchFailed=false launchCount=1 lastLaunchTime=-23s446ms
          haveState=false icicle=null
          state=RESUMED stopped=false delayedResume=false finishing=false
          keysPaused=false inHistory=true visible=true sleeping=false idle=true mStartingWindowState=STARTING_WINDOW_NOT_SHOWN
          fullscreen=true noDisplay=false immersive=false launchMode=0
          frozenBeforeDestroy=false forceNewConfig=false
          mActivityType=APPLICATION_ACTIVITY_TYPE
          waitingVisible=false nowVisible=true lastVisibleTime=-22s952ms
          resizeMode=RESIZE_MODE_RESIZEABLE
      * Hist #0: ActivityRecord{b786d24 u0 com.example.flag/.MainActivity t2493}
          packageName=com.example.flag processName=com.example.flag
          launchedFromUid=2000 launchedFromPackage=null userId=0
          app=ProcessRecord{b30f2d1 11785:com.example.flag/u0a63}
          Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.flag/.MainActivity }
          frontOfTask=true task=TaskRecord{cb64df8 #2493 A=com.example.flag U=0 StackId=1 sz=2}
          taskAffinity=com.example.flag
          realActivity=com.example.flag/.MainActivity
          baseDir=/data/app/com.example.flag-1/base.apk
          dataDir=/data/user/0/com.example.flag
          stateNotNeeded=false componentSpecified=true mActivityType=0
          compat={480dpi} labelRes=0x7f060021 icon=0x7f030000 theme=0x7f0800a3
          config={1.0 460mcc1mnc [zh_CN_#Hans,en_US,zh_TW_#Hant] ldltr sw360dp w360dp h616dp 480dpi nrml long port finger -keyb/v/h -nav/h s.8}
          taskConfigOverride={1.0 ?mcc?mnc ?localeList ?layoutDir ?swdp ?wdp ?hdp ?density ?lsize ?long ?orien ?uimode ?night ?touch ?keyb/?/? ?nav/?}
          taskDescription: iconFilename=null label="null" color=ff3f51b5
          launchFailed=false launchCount=0 lastLaunchTime=-57s646ms
          haveState=true icicle=Bundle[mParcelledData.dataSize=604]
          state=STOPPED stopped=true delayedResume=false finishing=false
          keysPaused=false inHistory=true visible=false sleeping=false idle=true mStartingWindowState=STARTING_WINDOW_SHOWN
          fullscreen=true noDisplay=false immersive=false launchMode=0
          frozenBeforeDestroy=false forceNewConfig=false
          mActivityType=APPLICATION_ACTIVITY_TYPE
          waitingVisible=false nowVisible=false lastVisibleTime=-56s921ms
          resizeMode=RESIZE_MODE_RESIZEABLE
android
asked on Stack Overflow Jul 10, 2017 by Scott • edited Jul 10, 2017 by Ajeet Choudhary

4 Answers

3

This Flag Only Starts an Activity in a new Task If it's not previously started and added to the stack.If the Activity to be started is on the Backstack then a new activity will not be started; instead, the current task will simply be brought to the front of the screen with the state it was last in.If you are starting an Activity which is not previously or not i the backstack then will create a new instance of the Activity in a new Task.

Although If you want to always Start a new Activity then you can you this,

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|FLAG_ACTIVITY_MULTIPLE_TASK)

This will always start the Activity in a new Task.

answered on Stack Overflow Aug 28, 2018 by Shubham Soni
1

You need to add combination of flags:

Intent intent = new Intent(this, Main2Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
answered on Stack Overflow Jul 10, 2017 by nitinkumarp
1

This is how you do it:

<activity
    android:name=".Main2Activity"
    android:taskAffinity="com.some.new" />
answered on Stack Overflow Nov 6, 2018 by dassa • edited Nov 6, 2018 by Ümañg ßürmån
1

Ümañg ßürmån's answer is correct.

    <activity
        android:name=".Main2Activity"
        android:taskAffinity="com.some.new" />

You must set the "android:taskAffinity" in the manifest in addition to setting the flag Intent.FLAG_ACTIVITY_NEW_TASK in order to have two tasks.

It was voted down to a -1 here for some reason when I found this.

https://developer.android.com/guide/topics/manifest/activity-element.html#aff

answered on Stack Overflow Aug 16, 2019 by daveb

User contributions licensed under CC BY-SA 3.0