Activity task问题(一)现象:Activity task栈顺序,而非Activity在task中的顺序,是和最后启动的Activity有关的

在上周五解决了一个非常棘手的bug,由于上周六日没时间整理,现在把整个过程有价值的东西整理沉淀下来。主要是笔记用。

首先现在在家里没有公司的代码,bug具体走过的代码无法完整描述出来。现在先记个大概,周一再详细描述清楚。

程序背景:两个应用,调用者:com.example.root.dolaunch;被调用者:com.example.root.belaunched。dolaunch就是简单地用inten去调belaunched。belaunched中,FirstActivity被调起后,延时一段时间(没别的用,纯粹是为了让你们看一眼,啊,FirstActivity确实被调起来了,不延时看不到)后,先后调起SecondActivity和ThirdActivity,SecondActivity是singleTask的。

现象:FirstActivity被调起,4s后自动销毁,之后ThirdActivity调起,按back键后,ThirdActivity销毁,返回DoLaunch,再按back键后,DoLaunch销毁,返回SecondActivity。但是FirstActivity、SecondActivity、ThirdActivity是在同一应用中的,逻辑上希望的是:FirstActivity销毁后,呈现ThirdActivity,back键后,返回SecondActivity,而不是DoLaunch。下面是现象、log以及Activity task信息。根据Activity栈信息,DoLaunch始终在SecondActivity的前面。可见,SecondActivity由于是singleTask的并且taskAffinity跟DoLaunch不同,所以单独一个task。FirstActivity、ThirdActivity、DoLaunch均在同一个task。



FirstActivity  finish前:

ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)
Display #0 (activities from top to bottom):
  Stack #6:
    Task id #13
    * TaskRecord{f167a15 #13 A=com.example.root.dolaunch U=0 sz=2}
      userId=0 effectiveUid=u0a62 mCallingUid=u0a12 mCallingPackage=com.android.launcher3
      affinity=com.example.root.dolaunch
      intent={act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.root.dolaunch/.DoLaunch bnds=[1103,364
][1327,588]}
     ......
      * Hist #1: ActivityRecord{486c507 u0 com.example.root.belaunched/.FirstActivity t13}
          packageName=com.example.root.belaunched processName=com.example.root.belaunched
          launchedFromUid=10062 launchedFromPackage=com.example.root.dolaunch userId=0
          app=ProcessRecord{e5cb16b 13246:com.example.root.belaunched/u0a63}
          Intent { act=android.belaunched.action.MAIN cmp=com.example.root.belaunched/.FirstActivity }
          ......
      * Hist #0: ActivityRecord{87e3d35 u0 com.example.root.dolaunch/.DoLaunch t13}
          packageName=com.example.root.dolaunch processName=com.example.root.dolaunch
          launchedFromUid=10012 launchedFromPackage=com.android.launcher3 userId=0
          app=ProcessRecord{702912a 14984:com.example.root.dolaunch/u0a62}
          Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.root.dolaunch/.DoLaunch bnds=[110
3,364][1327,588] (has extras) }
          ......
    Task id #12
    * TaskRecord{6513961 #12 A=com.example.root.belaunched U=0 sz=1}
      userId=0 effectiveUid=u0a63 mCallingUid=u0a63 mCallingPackage=com.example.root.belaunched
      affinity=com.example.root.belaunched
      intent={flg=0x10000000 cmp=com.example.root.belaunched/.SecondActivity}
      ......
      * Hist #0: ActivityRecord{3e6a703 u0 com.example.root.belaunched/.SecondActivity t12}
          packageName=com.example.root.belaunched processName=com.example.root.belaunched
          launchedFromUid=10063 launchedFromPackage=com.example.root.belaunched userId=0
          app=null
          Intent { flg=0x10000000 cmp=com.example.root.belaunched/.SecondActivity }
          ......

finish后:

ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)
Display #0 (activities from top to bottom):
  Stack #6:
    Task id #13
    * TaskRecord{f167a15 #13 A=com.example.root.dolaunch U=0 sz=2}
      userId=0 effectiveUid=u0a62 mCallingUid=u0a12 mCallingPackage=com.android.launcher3
      affinity=com.example.root.dolaunch
      intent={act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.root.dolaunch/.DoLaunch bnds=[1103,364
][1327,588]}
      ......
      * Hist #1: ActivityRecord{69095f6 u0 com.example.root.belaunched/.ThirdActivity t13}
          packageName=com.example.root.belaunched processName=com.example.root.belaunched
          launchedFromUid=10063 launchedFromPackage=com.example.root.belaunched userId=0
          app=ProcessRecord{e5cb16b 13246:com.example.root.belaunched/u0a63}
          Intent { cmp=com.example.root.belaunched/.ThirdActivity }
          ......
      * Hist #0: ActivityRecord{87e3d35 u0 com.example.root.dolaunch/.DoLaunch t13}
          packageName=com.example.root.dolaunch processName=com.example.root.dolaunch
          launchedFromUid=10012 launchedFromPackage=com.android.launcher3 userId=0
          app=ProcessRecord{702912a 14984:com.example.root.dolaunch/u0a62}
          Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.root.dolaunch/.DoLaunch bnds=[110
3,364][1327,588] (has extras) }
          ......
    Task id #12
    * TaskRecord{6513961 #12 A=com.example.root.belaunched U=0 sz=1}
      userId=0 effectiveUid=u0a63 mCallingUid=u0a63 mCallingPackage=com.example.root.belaunched
      affinity=com.example.root.belaunched
      intent={flg=0x10000000 cmp=com.example.root.belaunched/.SecondActivity}
      ......
      * Hist #0: ActivityRecord{3e6a703 u0 com.example.root.belaunched/.SecondActivity t12}
          packageName=com.example.root.belaunched processName=com.example.root.belaunched
          launchedFromUid=10063 launchedFromPackage=com.example.root.belaunched userId=0
          app=null
          Intent { flg=0x10000000 cmp=com.example.root.belaunched/.SecondActivity }
          ......

public class DoLaunch extends Activity {

    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_do_launch);
        button = (Button)findViewById(R.id.btnLaunch);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClassName("com.example.root.belaunched",
                        "com.example.root.belaunched.FirstActivity");
                intent.setAction("android.belaunched.action.MAIN");
                startActivity(intent);
            }
        });
        Toast.makeText(this, "Begin!", Toast.LENGTH_LONG).show();
        Log.d("vfp8341","DoLaunch:taskId:" + getTaskId());
    }
}




    
            
                

                
            
        
    



public class FirstActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_be_launched);
        Handler handler = new Handler();
        handler.postDelayed(startActivity, 4000);
        Log.d("vfp8341","FirstActivity:taskId:" + getTaskId());
    }

    Runnable startActivity = new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent();
            intent.setClassName("com.example.root.belaunched",
                    "com.example.root.belaunched.SecondActivity");
            startActivity(intent);

            intent.setClassName("com.example.root.belaunched",
                    "com.example.root.belaunched.ThirdActivity");
            startActivity(intent);
            FirstActivity.this.finish();
        }
    };
}

public class SecondActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);
        Log.d("vfp8341","SecondActivity:taskId:" + getTaskId());
    }
}

public class ThirdActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.third_activity);
        Log.d("vfp8341","ThirdActivity:taskId:" + getTaskId());
    }
}




    
            
                

                
            
        
        
        
        
        
    


分析:把FirstActivity的run函数改为(仅仅是顺序颠倒):

    intent.setClassName("com.example.root.belaunched",
            "com.example.root.belaunched.ThirdActivity");
    startActivity(intent);
    
    Intent intent = new Intent();
    intent.setClassName("com.example.root.belaunched",
            "com.example.root.belaunched.SecondActivity");
    startActivity(intent);


Activity task问题(一)现象:Activity task栈顺序,而非Activity在task中的顺序,是和最后启动的Activity有关的_第1张图片
则可以发现,SecondActivity所在的task跑到前台了。ThirdActivity以及DoLaunch还是同在一个task,但是他们所在的task在SecondActivity所在task的下面。

ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)
Display #0 (activities from top to bottom):
  Stack #11:
    Task id #20
    * TaskRecord{220858b #20 A=com.example.root.belaunched U=0 sz=1}
      userId=0 effectiveUid=u0a63 mCallingUid=u0a63 mCallingPackage=com.example.root.belaunched
      affinity=com.example.root.belaunched
      intent={flg=0x10000000 cmp=com.example.root.belaunched/.SecondActivity}
      ......
      * Hist #0: ActivityRecord{8b4ba7f u0 com.example.root.belaunched/.SecondActivity t20}
          packageName=com.example.root.belaunched processName=com.example.root.belaunched
          launchedFromUid=10063 launchedFromPackage=com.example.root.belaunched userId=0
          app=ProcessRecord{50b6668 7344:com.example.root.belaunched/u0a63}
          Intent { flg=0x10000000 cmp=com.example.root.belaunched/.SecondActivity }
          ......
    Task id #19
    * TaskRecord{b7e7a81 #19 A=com.example.root.dolaunch U=0 sz=2}
      userId=0 effectiveUid=u0a62 mCallingUid=u0a12 mCallingPackage=com.android.launcher3
      affinity=com.example.root.dolaunch
      intent={act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.root.dolaunch/.DoLaunch bnds=[1103,364
][1327,588]}
      ......
      * Hist #1: ActivityRecord{56e8c20 u0 com.example.root.belaunched/.ThirdActivity t19}
          packageName=com.example.root.belaunched processName=com.example.root.belaunched
          launchedFromUid=10063 launchedFromPackage=com.example.root.belaunched userId=0
          app=null
          Intent { cmp=com.example.root.belaunched/.ThirdActivity }
          ......
      * Hist #0: ActivityRecord{ddc8090 u0 com.example.root.dolaunch/.DoLaunch t19}
          packageName=com.example.root.dolaunch processName=com.example.root.dolaunch
          launchedFromUid=10012 launchedFromPackage=com.android.launcher3 userId=0
          app=ProcessRecord{ef3a726 7279:com.example.root.dolaunch/u0a62}
          Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.root.dolaunch/.DoLaunch bnds=[110
3,364][1327,588] (has extras) }
          ......

结论: 哪个Activity是最后start的,那么那个Activity所在的整个task都在前台,不管该task里包含的是哪些应用的Activity。

欢迎各路高手拍砖!

你可能感兴趣的:(Android)