使用标记FLAG_ACTIVITY_NEW_TASK启动活动,新活动没有在新任务中执行

时间:2017-07-10 07:31:37

标签: android

首先我使用Activity启动FLAG_ACTIVITY_NEW_TASK,然后使用命令' adb shell dumpsys Activity'最后我在一个任务中找到两个活动。

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

这些Activity是正常的,我没有修改它们。这些是堆栈信息。

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

5 个答案:

答案 0 :(得分:1)

  

此标志仅在新任务中启动活动   之前已启动并添加到堆栈中。如果活动为   启动是在Backstack上,则不会启动新的活动;   而是将当前任务简单地带到   屏幕上显示的是其上次进入的状态。   这不是以前还是不是我的堆栈然后将创建一个新的   新任务中的活动实例。

尽管如果您想始终启动新活动,则可以

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|FLAG_ACTIVITY_MULTIPLE_TASK)

这将始终在新任务中启动活动

答案 1 :(得分:1)

Ümañgßürmån的答案是正确的。

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

除了设置标志Intent.FLAG_ACTIVITY_NEW_TASK之外,您还必须在清单中设置“ android:taskAffinity”。

当我发现它时,出于某种原因,它在这里被否决为-1。

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

答案 2 :(得分:0)

您需要添加标志组合:

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

答案 3 :(得分:0)

我这样做是这样的:

val intent = Intent(this, DrawActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_MULTIPLE_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
startActivity(intent)

将这两行添加到清单的活动标签

android:launchMode="singleTask"
android:taskAffinity="com.draw.new"

<activity
  android:name=".activity.DrawingActivity"
  android:launchMode="singleTask"
  android:taskAffinity="com.draw.new"/>

答案 4 :(得分:-1)

这是您的操作方式:

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