Android:启动模式总结

作者: 我在等你回复可你没回 | 来源:发表于2018-07-28 14:42 被阅读14次
    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。

    相关文章

      网友评论

        本文标题:Android:启动模式总结

        本文链接:https://www.haomeiwen.com/subject/ovsomftx.html