Android:启动模式总结

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 <code>singleTop</code> in
     * the {@link android.R.attr#launchMode} attribute.
     */
    public static final int LAUNCH_SINGLE_TOP = 1;
    /**
     * Constant corresponding to <code>singleTask</code> in
     * the {@link android.R.attr#launchMode} attribute.
     */
    public static final int LAUNCH_SINGLE_TASK = 2;
    /**
     * Constant corresponding to <code>singleInstance</code> 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是如下设置

        <activity android:name=".Sencond"
            android:launchMode="singleTask"
            android:taskAffinity="com.wenfengtou"/>

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?

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,277评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,689评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,624评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,356评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,402评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,292评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,135评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,992评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,429评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,636评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,785评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,492评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,092评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,723评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,858评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,891评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,713评论 2 354

推荐阅读更多精彩内容