Android:启动模式总结

Android:启动模式总结_第1张图片
timg.jpeg

android启动模式的实现一般分两种,一种在代码里面startActivity的时候添加flag实现,一种是在AndroidManifest中添加launchMode实现。 如果两个地方都加了,就会是叠加的效果。

一.flag例子:桌面点击图标是什么参数?

默认情况点击图标时:

ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.wenfengtou.firstmacapp/.MainActivity bnds=[539,717][790,1001]} from uid 10028 and from pid 2458

可以看到flag时0x10200000,对应的flag就是
FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_RESET_TASK_IF_NEEDED

FLAG_ACTIVITY_NEW_TASK标志,如果要启动的Activity在后台,就会把Activity所在的Task推到前台显示,要注意不一定显示要启动的Activity,而是显示Task上处于TOP位置的Activity。例如点击桌面图标,启动A,在A中启动B,点击home键返回桌面,再次点击桌面图标,会显示B,因为B处于TOP的位置。
使用adb模拟桌面点击:

am start -n com.example.wenfengtou.firstmacapp/.MainActivity -f  0x10200000 -c android.intent.category.LAUNCHER -a android.intent.action.MAIN                                   

注意一定注意保证action和catroy一样,否则会启动一下新的A,栈的情况就会是A-B-A

二.Launchmode例子:如何实现点击桌面图标会回到首页?

Launchmode有四种

    public static final int LAUNCH_MULTIPLE = 0;
    /**
     * Constant corresponding to singleTop in
     * the {@link android.R.attr#launchMode} attribute.
     */
    public static final int LAUNCH_SINGLE_TOP = 1;
    /**
     * Constant corresponding to singleTask in
     * the {@link android.R.attr#launchMode} attribute.
     */
    public static final int LAUNCH_SINGLE_TASK = 2;
    /**
     * Constant corresponding to singleInstance in
     * the {@link android.R.attr#launchMode} attribute.
     */
    public static final int LAUNCH_SINGLE_INSTANCE = 3;

可以通过dumpsys activity a 来查看启动的Activity的launchmode。

    Task id #28869
    mFullscreen=true
    mBounds=null
    mMinWidth=-1
    mMinHeight=-1
    mLastNonFullscreenBounds=null
    * TaskRecord{51e69b4 #28869 A=com.example.wenfengtou.firstmacapp U=0 StackId=1 sz=1}
      userId=0 effectiveUid=u0a218 mCallingUid=u0a218 mUserSetupComplete=true mCallingPackage=com.example.wenfengtou.firstmacapp
      affinity=com.example.wenfengtou.firstmacapp
      intent={flg=0x10104000 cmp=com.example.wenfengtou.firstmacapp/.Sencond}
      realActivity=com.example.wenfengtou.firstmacapp/.Sencond
      autoRemoveRecents=false isPersistable=true numFullscreen=1 taskType=0 mTaskToReturnTo=1
      rootWasReset=false mNeverRelinquishIdentity=true mReuseTask=false mLockTaskAuth=LOCK_TASK_AUTH_PINNABLE
      Activities=[ActivityRecord{8fcae2a u0 com.example.wenfengtou.firstmacapp/.Sencond t28869}]
      askedCompatMode=false inRecents=false isAvailable=true
      lastThumbnail=null lastThumbnailFile=/data/system_ce/0/recent_images/28869_task_thumbnail.png
      stackId=1
      hasBeenVisible=true mResizeMode=RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION mSupportsPictureInPicture=false isResizeable=true firstActiveTime=1532758042288 lastActiveTime=1532758200753 (inactive for 1310s)
      * Hist #0: ActivityRecord{8fcae2a u0 com.example.wenfengtou.firstmacapp/.Sencond t28869}
          packageName=com.example.wenfengtou.firstmacapp processName=com.example.wenfengtou.firstmacapp
          launchedFromUid=10218 launchedFromPackage=com.example.wenfengtou.firstmacapp userId=0
          app=ProcessRecord{cfd9c93 27860:com.example.wenfengtou.firstmacapp/u0a218}
          Intent { flg=0x10104000 cmp=com.example.wenfengtou.firstmacapp/.Sencond }
          frontOfTask=true task=TaskRecord{51e69b4 #28869 A=com.example.wenfengtou.firstmacapp U=0 StackId=1 sz=1}
          taskAffinity=com.example.wenfengtou.firstmacapp
          realActivity=com.example.wenfengtou.firstmacapp/.Sencond
          baseDir=/data/app/com.example.wenfengtou.firstmacapp-eLkTGBVSXt9xqMyC0HtQKQ==/base.apk
          dataDir=/data/user/0/com.example.wenfengtou.firstmacapp
          stateNotNeeded=false componentSpecified=true mActivityType=0
          compat={480dpi} labelRes=0x7f060021 icon=0x7f030000 theme=0x0
          mLastReportedConfigurations:
           mGlobalConfig={1.0 ?mcc?mnc [zh_CN] ldltr sw360dp w360dp h685dp 480dpi nrml long port finger -keyb/v/h -nav/h appBounds=Rect(0, 0 - 1080, 2136) s.28mThemeChanged = 0mThemeChangedFlags = 0mFlipFont = 0}
           mOverrideConfig={1.0 ?mcc?mnc [zh_CN] ldltr sw360dp w360dp h685dp 480dpi nrml long port finger -keyb/v/h -nav/h appBounds=Rect(0, 0 - 1080, 2136) s.28mThemeChanged = 0mThemeChangedFlags = 0mFlipFont = 0}
          CurrentConfiguration={1.0 ?mcc?mnc [zh_CN] ldltr sw360dp w360dp h685dp 480dpi nrml long port finger -keyb/v/h -nav/h appBounds=Rect(0, 0 - 1080, 2136) s.28mThemeChanged = 0mThemeChangedFlags = 0mFlipFont = 0}
          taskDescription: iconFilename=null label="null" primaryColor=ff212121
            backgroundColor=fffafafa
            statusBarColor=ff000000
            navigationBarColor=0
          launchFailed=false launchCount=0 lastLaunchTime=-22m24s866ms
          haveState=true icicle=Bundle[mParcelledData.dataSize=860]
          state=STOPPED stopped=true delayedResume=false finishing=false
          keysPaused=false inHistory=true visible=false sleeping=false idle=true mStartingWindowState=STARTING_WINDOW_REMOVED
          fullscreen=true noDisplay=false immersive=false launchMode=0

四种launchmode简介:
standard:普通模式,启动activity都会新建。
singletop:如果要启动的Activity在top了,就不会新建Activity。如果不在,就新建一个。
singletask:如果要启动的activity在top了,就不会新建Activity(这种情况跟singletop一样)。如果不在top,而是在中间,就会清空他头上的所有activity。如果没创建过该activity,就会新建一个在top。
singleinstance:会在一个单独的task里面。

所以,如何如何实现点击桌面图标会回到首页?
例如首页时A,然后A启动了B,B启动了C。然后点击home键,如何实现点击桌面图片回到A呢?

答案就是设置A的Launchmode为singletask就可以了。因为他会清空他头上的B和C。

平安产险:

一个应用可以有多个task吗?

可以。
方式有两种:
1.启动一个launchmode为singleinstance的activity,就用启动一个新的task。
2.启动一个亲和度不一样的activity,并且是这个activity的launchmode是singletask(或者intent的flag设置成FLAG_ACTIVITY_NEW_TASK)

com.example.wenfengtou.firstmacapp/.Sencond就是设置了singletask并且亲和度为com.wenfengtou的Activity。可以看到跟
com.example.wenfengtou.firstmacapp/.MainActivity在不同的Task里面。一个在Task #29,一个在Task #28。

    Task id #29
    mFullscreen=true
    mBounds=null
    mMinWidth=-1
    mMinHeight=-1
    mLastNonFullscreenBounds=null
    * TaskRecord{1e02dad #29 A=com.wenfengtou U=0 StackId=1 sz=1}
      userId=0 effectiveUid=u0a84 mCallingUid=u0a84 mUserSetupComplete=true mCallingPackage=com.example.wenfengtou.firstmacapp
      affinity=com.wenfengtou
      intent={flg=0x10000000 cmp=com.example.wenfengtou.firstmacapp/.Sencond}
      realActivity=com.example.wenfengtou.firstmacapp/.Sencond
      autoRemoveRecents=false isPersistable=true numFullscreen=1 taskType=0 mTaskToReturnTo=0
      rootWasReset=false mNeverRelinquishIdentity=true mReuseTask=false mLockTaskAuth=LOCK_TASK_AUTH_PINNABLE
      Activities=[ActivityRecord{8e058d0 u0 com.example.wenfengtou.firstmacapp/.Sencond t29}]
      askedCompatMode=false inRecents=true isAvailable=true
      lastThumbnail=null lastThumbnailFile=/data/system_ce/0/recent_images/29_task_thumbnail.png
      stackId=1
      hasBeenVisible=true mResizeMode=RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION mSupportsPictureInPicture=false isResizeable=true firstActiveTime=1553332838382 lastActiveTime=1553332838382 (inactive for 12s)
      * Hist #0: ActivityRecord{8e058d0 u0 com.example.wenfengtou.firstmacapp/.Sencond t29}
          packageName=com.example.wenfengtou.firstmacapp processName=com.example.wenfengtou.firstmacapp
          launchedFromUid=10084 launchedFromPackage=com.example.wenfengtou.firstmacapp userId=0
          app=ProcessRecord{b2c58e2 20808:com.example.wenfengtou.firstmacapp/u0a84}
          Intent { flg=0x10000000 cmp=com.example.wenfengtou.firstmacapp/.Sencond }
          frontOfTask=true task=TaskRecord{1e02dad #29 A=com.wenfengtou U=0 StackId=1 sz=1}
          taskAffinity=com.wenfengtou
          realActivity=com.example.wenfengtou.firstmacapp/.Sencond
          baseDir=/data/app/com.example.wenfengtou.firstmacapp-MLcE0o9GmJ6AneQdbCfeQw==/base.apk
          dataDir=/data/user/0/com.example.wenfengtou.firstmacapp
          splitDir=[/data/app/com.example.wenfengtou.firstmacapp-MLcE0o9GmJ6AneQdbCfeQw==/split_lib_dependencies_apk.apk, /data/app/com.example.wenfengtou.firstmacapp-MLcE0o9GmJ6AneQdbCfeQw==/split_lib_slice_0_apk.apk, /data/app/com.example.wenfengtou.firstmacapp-MLcE0o9GmJ6AneQdbCfeQw==/split_lib_slice_1_apk.apk, /data/app/com.example.wenfengtou.firstmacapp-MLcE0o9GmJ6AneQdbCfeQw==/split_lib_slice_2_apk.apk, /data/app/com.example.wenfengtou.firstmacapp-MLcE0o9GmJ6AneQdbCfeQw==/split_lib_slice_3_apk.apk, /data/app/com.example.wenfengtou.firstmacapp-MLcE0o9GmJ6AneQdbCfeQw==/split_lib_slice_4_apk.apk, /data/app/com.example.wenfengtou.firstmacapp-MLcE0o9GmJ6AneQdbCfeQw==/split_lib_slice_5_apk.apk, /data/app/com.example.wenfengtou.firstmacapp-MLcE0o9GmJ6AneQdbCfeQw==/split_lib_slice_6_apk.apk, /data/app/com.example.wenfengtou.firstmacapp-MLcE0o9GmJ6AneQdbCfeQw==/split_lib_slice_7_apk.apk, /data/app/com.example.wenfengtou.firstmacapp-MLcE0o9GmJ6AneQdbCfeQw==/split_lib_slice_8_apk.apk, /data/app/com.example.wenfengtou.firstmacapp-MLcE0o9GmJ6AneQdbCfeQw==/split_lib_slice_9_apk.apk]
          stateNotNeeded=false componentSpecified=true mActivityType=0
          compat={420dpi} labelRes=0x7f060021 icon=0x7f030000 theme=0x0
          mLastReportedConfigurations:
           mGlobalConfig={1.0 310mcc260mnc [en_US] ldltr sw411dp w411dp h659dp 420dpi nrml port finger qwerty/v/v -nav/h appBounds=Rect(0, 0 - 1080, 1794) s.6}
           mOverrideConfig={1.0 310mcc260mnc [en_US] ldltr sw411dp w411dp h659dp 420dpi nrml port finger qwerty/v/v -nav/h appBounds=Rect(0, 0 - 1080, 1794) s.6}
          CurrentConfiguration={1.0 310mcc260mnc [en_US] ldltr sw411dp w411dp h659dp 420dpi nrml port finger qwerty/v/v -nav/h appBounds=Rect(0, 0 - 1080, 1794) s.6}
          taskDescription: iconFilename=null label="null" primaryColor=ff212121
           backgroundColor=fffafafa
           statusBarColor=ff000000
           navigationBarColor=ff000000
          launchFailed=false launchCount=1 lastLaunchTime=-12s339ms
          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_SHOWN
          fullscreen=true noDisplay=false immersive=false launchMode=2
          frozenBeforeDestroy=false forceNewConfig=false
          mActivityType=APPLICATION_ACTIVITY_TYPE
          waitingVisible=false nowVisible=true lastVisibleTime=-11s158ms
          resizeMode=RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION
          mLastReportedMultiWindowMode=false mLastReportedPictureInPictureMode=false
    Task id #28
    mFullscreen=true
    mBounds=null
    mMinWidth=-1
    mMinHeight=-1
    mLastNonFullscreenBounds=null
    * TaskRecord{5c2e473 #28 A=com.example.wenfengtou.firstmacapp U=0 StackId=1 sz=1}
      userId=0 effectiveUid=u0a84 mCallingUid=2000 mUserSetupComplete=true mCallingPackage=null
      affinity=com.example.wenfengtou.firstmacapp
      intent={act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.wenfengtou.firstmacapp/.MainActivity}
      realActivity=com.example.wenfengtou.firstmacapp/.MainActivity
      autoRemoveRecents=false isPersistable=true numFullscreen=1 taskType=0 mTaskToReturnTo=1
      rootWasReset=false mNeverRelinquishIdentity=true mReuseTask=false mLockTaskAuth=LOCK_TASK_AUTH_PINNABLE
      Activities=[ActivityRecord{c281c10 u0 com.example.wenfengtou.firstmacapp/.MainActivity t28}]
      askedCompatMode=false inRecents=true isAvailable=true
      lastThumbnail=null lastThumbnailFile=/data/system_ce/0/recent_images/28_task_thumbnail.png
      stackId=1
      hasBeenVisible=true mResizeMode=RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION mSupportsPictureInPicture=false isResizeable=true firstActiveTime=1553332838281 lastActiveTime=1553332838281 (inactive for 12s)
      * Hist #0: ActivityRecord{c281c10 u0 com.example.wenfengtou.firstmacapp/.MainActivity t28}
          packageName=com.example.wenfengtou.firstmacapp processName=com.example.wenfengtou.firstmacapp
          launchedFromUid=2000 launchedFromPackage=null userId=0
          app=ProcessRecord{b2c58e2 20808:com.example.wenfengtou.firstmacapp/u0a84}
          Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.wenfengtou.firstmacapp/.MainActivity }
          frontOfTask=true task=TaskRecord{5c2e473 #28 A=com.example.wenfengtou.firstmacapp U=0 StackId=1 sz=1}
          taskAffinity=com.example.wenfengtou.firstmacapp
          realActivity=com.example.wenfengtou.firstmacapp/.MainActivity
          baseDir=/data/app/com.example.wenfengtou.firstmacapp-MLcE0o9GmJ6AneQdbCfeQw==/base.apk
          dataDir=/data/user/0/com.example.wenfengtou.firstmacapp
          splitDir=[/data/app/com.example.wenfengtou.firstmacapp-MLcE0o9GmJ6AneQdbCfeQw==/split_lib_dependencies_apk.apk, /data/app/com.example.wenfengtou.firstmacapp-MLcE0o9GmJ6AneQdbCfeQw==/split_lib_slice_0_apk.apk, /data/app/com.example.wenfengtou.firstmacapp-MLcE0o9GmJ6AneQdbCfeQw==/split_lib_slice_1_apk.apk, /data/app/com.example.wenfengtou.firstmacapp-MLcE0o9GmJ6AneQdbCfeQw==/split_lib_slice_2_apk.apk, /data/app/com.example.wenfengtou.firstmacapp-MLcE0o9GmJ6AneQdbCfeQw==/split_lib_slice_3_apk.apk, /data/app/com.example.wenfengtou.firstmacapp-MLcE0o9GmJ6AneQdbCfeQw==/split_lib_slice_4_apk.apk, /data/app/com.example.wenfengtou.firstmacapp-MLcE0o9GmJ6AneQdbCfeQw==/split_lib_slice_5_apk.apk, /data/app/com.example.wenfengtou.firstmacapp-MLcE0o9GmJ6AneQdbCfeQw==/split_lib_slice_6_apk.apk, /data/app/com.example.wenfengtou.firstmacapp-MLcE0o9GmJ6AneQdbCfeQw==/split_lib_slice_7_apk.apk, /data/app/com.example.wenfengtou.firstmacapp-MLcE0o9GmJ6AneQdbCfeQw==/split_lib_slice_8_apk.apk, /data/app/com.example.wenfengtou.firstmacapp-MLcE0o9GmJ6AneQdbCfeQw==/split_lib_slice_9_apk.apk]
          stateNotNeeded=false componentSpecified=true mActivityType=0
          compat={420dpi} labelRes=0x7f060021 icon=0x7f030000 theme=0x0
          mLastReportedConfigurations:
           mGlobalConfig={1.0 310mcc260mnc [en_US] ldltr sw411dp w411dp h659dp 420dpi nrml port finger qwerty/v/v -nav/h appBounds=Rect(0, 0 - 1080, 1794) s.6}
           mOverrideConfig={1.0 310mcc260mnc [en_US] ldltr sw411dp w411dp h659dp 420dpi nrml port finger qwerty/v/v -nav/h appBounds=Rect(0, 0 - 1080, 1794) s.6}
          CurrentConfiguration={1.0 310mcc260mnc [en_US] ldltr sw411dp w411dp h659dp 420dpi nrml port finger qwerty/v/v -nav/h appBounds=Rect(0, 0 - 1080, 1794) s.6}
          taskDescription: iconFilename=null label="null" primaryColor=ff212121
           backgroundColor=fffafafa
           statusBarColor=ff000000
           navigationBarColor=ff000000
          launchFailed=false launchCount=0 lastLaunchTime=-19s42ms
          haveState=true icicle=Bundle[mParcelledData.dataSize=936]
          state=STOPPED stopped=true delayedResume=false finishing=false
          keysPaused=false inHistory=true visible=false sleeping=false idle=true mStartingWindowState=STARTING_WINDOW_REMOVED
          fullscreen=true noDisplay=false immersive=false launchMode=1
          frozenBeforeDestroy=false forceNewConfig=false
          mActivityType=APPLICATION_ACTIVITY_TYPE
          waitingVisible=false nowVisible=false lastVisibleTime=-16s673ms
          resizeMode=RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION
          mLastReportedMultiWindowMode=false mLastReportedPictureInPictureMode=false

AndroidManifest是如下设置

        

SingleInstance的task中只能有一个activity 吗?

对的。

一个ActivityA的taskAffinity为com.wenfengtou,Activity所在的Task的taskAffinity也为com.wenfengtou,那么他启动一个另一个taskAffinity的ActivityB,这个Activity会在哪个Task里面?

如果ActiviyB的启动模式是standard,会在ActivityA的TASK里面。
如果ActivityB的启动模式是SingleTask,就不回在ActivitA的TASK里面。

Activity切换时先执行onstop还是onstart?

你可能感兴趣的:(Android:启动模式总结)