您的位置:首页 > 移动开发 > Android开发

Android之FLAG_ACTIVITY_SINGLE_TOP

2016-06-30 10:12 459 查看
FLAG_ACTIVITY_SINGLE_TOP :如果当前栈顶的activity就是要启动的activity,则不会再启动一个新的activity

实例:

我们有一个apk,apk中包含两个Activity:MainActivity和ActivityA,点击MainActivity启动ActivityA,点击ActivityA还是启动ActivityA,但我们设置FLAG_ACTIVITY_SINGLE_TOP标记:

[java] view plain copy

print?

    public void onClick(View v) {  

        // TODO Auto-generated method stub  

        Log.i(TAG, "--onClick--task id = " + getCurrentTaskId());     

          

        Intent intent = new Intent("com.leaves.ipanel.ActivityA");    

        //intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK);  

        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);  

        startActivity(intent);   

    }  

         

启动动ActivityA,随便我们怎么再点击,都只有一个ActivityA实例,堆栈:

[plain] view plain copy

print?

    ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)  

      Main stack:  

        TaskRecord{438f1ed8 #9 A com.leaves.ipanel U 0}  

        Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.leaves.ipanel/.MainActivity }  

          Hist #2: ActivityRecord{4265b1b0 u0 com.leaves.ipanel/.ActivityA}  

            Intent { act=com.leaves.ipanel.ActivityA flg=0x20000000 cmp=com.leaves.ipanel/.ActivityA }  

            ProcessRecord{4292a550 2115:com.leaves.ipanel/u0a10061}  

          Hist #1: ActivityRecord{42485758 u0 com.leaves.ipanel/.MainActivity}  

            Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.leaves.ipanel/.MainActivity }  

            ProcessRecord{4292a550 2115:com.leaves.ipanel/u0a10061}  

        TaskRecord{426f4820 #2 A com.android.launcher U 0}  

        Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10600000 cmp=com.android.launcher/com.android.launcher2.Launcher }  

          Hist #0: ActivityRecord{4291c7b0 u0 com.android.launcher/com.android.launcher2.Launcher}  

            Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10000000 cmp=com.android.launcher/com.android.launcher2.Launcher }  

            ProcessRecord{4267f0b8 636:com.android.launcher/1000} 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: