为了想弄懂allowTaskReparenting,这几天看太多网上的文章,然后照着文章的实例码了一下,但还是试不出效果,其实归根结底都还是对这概念理解不够透彻。
于是按照自己的想法写了两个demo的应用,终于有点效果,废话不多说,直接看代码。
App1的代码如下:
public class App1Act1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act1);
findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(App1Act1.this, App1Act2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
}
}
public class App1Act2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act2);
findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.kv.testme");
startActivity(intent);
}
});
}
}
App2的代码如下:
public class App2Act1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act1);
findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(App2Act1.this, App2Act2.class);
startActivity(intent);
}
});
}
}
public class App2Act2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act2);
}
}
于是操作步骤为:
启动App1,启动了App1Act1,点击进入App1Act2,再点击调用了App2的activity,App2Act2。
adb shell dumpsys activity
结果如下
Task id #13828
TaskRecord{9214ad #13828 A=com.kf.app U=0 sz=2}
Intent { flg=0x10000000 cmp=com.kv.app1/.App1Act2 }
Hist #1: ActivityRecord{92909d8 u0 com.kv.app2/.App2Act2 t13828}
Intent { act=com.kv.testme cmp=com.kv.app2/.App2Act2 }
ProcessRecord{2e3f1818 31397:com.kv.app2/u0a1060}
Hist #0: ActivityRecord{32b8a59a u0 com.kv.app1/.App1Act2 t13828}
Intent { flg=0x10000000 cmp=com.kv.app1/.App1Act2 }
ProcessRecord{cd3d52c 31365:com.kv.app1/u0a1059}
Task id #13827
TaskRecord{2b4db3e2 #13827 A=com.kv.app1 U=0 sz=1}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x30200000 cmp=com.kv.app1/.App1Act1 }
Hist #0: ActivityRecord{38e9bc1d u0 com.kv.app1/.App1Act1 t13827}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x30200000 cmp=com.kv.app1/.App1Act1 bnds=[21,86][273,371] }
ProcessRecord{cd3d52c 31365:com.kv.app1/u0a1059}
返回桌面,启动App2,发现App2的Act2Act2被打开了。
dumpsys activity结果如下:
Task id #13832
TaskRecord{3bfe2e7b #13832 A=com.kv.app2 U=0 sz=2}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x30200000 cmp=com.kv.app2/.App2Act1 }
Hist #1: ActivityRecord{27d7fc u0 com.kv.app2/.App2Act2 t13832}
Intent { act=com.kv.testme cmp=com.kv.app2/.App2Act2 }
ProcessRecord{31773b5f 32563:com.kv.app2/u0a1060}
Hist #0: ActivityRecord{ff9cceb u0 com.kv.app2/.App2Act1 t13832}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x30200000 cmp=com.kv.app2/.App2Act1 bnds=[283,86][535,371] }
Task id #13831
TaskRecord{2b857498 #13831 A=com.kf.app U=0 sz=1}
Intent { flg=0x10000000 cmp=com.kv.app1/.App1Act2 }
Hist #0: ActivityRecord{9aac9ff u0 com.kv.app1/.App1Act2 t13831}
Intent { flg=0x10000000 cmp=com.kv.app1/.App1Act2 }
ProcessRecord{fe537b9 32532:com.kv.app1/u0a1059}
Task id #13830
TaskRecord{36e924f1 #13830 A=com.kv.app1 U=0 sz=1}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x30200000 cmp=com.kv.app1/.App1Act1 }
Hist #0: ActivityRecord{35c8a417 u0 com.kv.app1/.App1Act1 t13830}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x30200000 cmp=com.kv.app1/.App1Act1 bnds=[21,86][273,371] }
ProcessRecord{fe537b9 32532:com.kv.app1/u0a1059}
如果将allowTaskReparenting设置为false呢?接着还是上面的步骤。
启动App1,启动了App1Act1,点击进入App1Act2,再点击调用了App2的activity,App2Act2。
敲dumpsys activity查看结果
Task id #13836
TaskRecord{6be6790 #13836 A=com.kf.app U=0 sz=2}
Intent { flg=0x10000000 cmp=com.kv.app1/.App1Act2 }
Hist #1: ActivityRecord{500f6ba u0 com.kv.app2/.App2Act2 t13836}
Intent { act=com.kv.testme cmp=com.kv.app2/.App2Act2 }
ProcessRecord{25b7b253 2602:com.kv.app2/u0a1060}
Hist #0: ActivityRecord{212508fe u0 com.kv.app1/.App1Act2 t13836}
Intent { flg=0x10000000 cmp=com.kv.app1/.App1Act2 }
ProcessRecord{3eb6108d 2550:com.kv.app1/u0a1059}
Task id #13835
TaskRecord{c002889 #13835 A=com.kv.app1 U=0 sz=1}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x30200000 cmp=com.kv.app1/.App1Act1 }
Hist #0: ActivityRecord{34e0fe77 u0 com.kv.app1/.App1Act1 t13835}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x30200000 cmp=com.kv.app1/.App1Act1 bnds=[21,86][273,371] }
ProcessRecord{3eb6108d 2550:com.kv.app1/u0a1059}
和上面未启动App2时一样的结果,那么点击home退回桌面,如果这个时候启动App2,会是什么样的结果呢?
如下:Task id #13839
TaskRecord{96f96e8 #13839 A=com.kv.app2 U=0 sz=1}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x30200000 cmp=com.kv.app2/.App2Act1 }
Hist #0: ActivityRecord{314c4e44 u0 com.kv.app2/.App2Act1 t13839}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x30200000 cmp=com.kv.app2/.App2Act1 bnds=[283,86][535,371] }
ProcessRecord{25b7b253 2602:com.kv.app2/u0a1060}
Task id #13838
TaskRecord{23ee2101 #13838 A=com.kf.app U=0 sz=2}
Intent { flg=0x10000000 cmp=com.kv.app1/.App1Act2 }
Hist #1: ActivityRecord{3980b814 u0 com.kv.app2/.App2Act2 t13838}
Intent { act=com.kv.testme cmp=com.kv.app2/.App2Act2 }
ProcessRecord{25b7b253 2602:com.kv.app2/u0a1060}
Hist #0: ActivityRecord{39af1376 u0 com.kv.app1/.App1Act2 t13838}
Intent { flg=0x10000000 cmp=com.kv.app1/.App1Act2 }
ProcessRecord{22ad280b 3174:com.kv.app1/u0a1059}
Task id #13837
TaskRecord{22388ba6 #13837 A=com.kv.app1 U=0 sz=1}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x30200000 cmp=com.kv.app1/.App1Act1 }
Hist #0: ActivityRecord{1f089f69 u0 com.kv.app1/.App1Act1 t13837}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x30200000 cmp=com.kv.app1/.App1Act1 bnds=[21,86][273,371] }
ProcessRecord{22ad280b 3174:com.kv.app1/u0a1059}
如果App2上,App2Act1界面点击进入App2Act2呢?结果如下:
Task id #13839
TaskRecord{96f96e8 #13839 A=com.kv.app2 U=0 sz=2}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x30200000 cmp=com.kv.app2/.App2Act1 }
Hist #1: ActivityRecord{4c04c71 u0 com.kv.app2/.App2Act2 t13839}
Intent { cmp=com.kv.app2/.App2Act2 }
ProcessRecord{25b7b253 2602:com.kv.app2/u0a1060}
Hist #0: ActivityRecord{314c4e44 u0 com.kv.app2/.App2Act1 t13839}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x30200000 cmp=com.kv.app2/.App2Act1 bnds=[283,86][535,371] }
ProcessRecord{25b7b253 2602:com.kv.app2/u0a1060}
Task id #13838
TaskRecord{23ee2101 #13838 A=com.kf.app U=0 sz=2}
Intent { flg=0x10000000 cmp=com.kv.app1/.App1Act2 }
Hist #1: ActivityRecord{3980b814 u0 com.kv.app2/.App2Act2 t13838}
Intent { act=com.kv.testme cmp=com.kv.app2/.App2Act2 }
ProcessRecord{25b7b253 2602:com.kv.app2/u0a1060}
Hist #0: ActivityRecord{39af1376 u0 com.kv.app1/.App1Act2 t13838}
Intent { flg=0x10000000 cmp=com.kv.app1/.App1Act2 }
ProcessRecord{22ad280b 3174:com.kv.app1/u0a1059}
Task id #13837
TaskRecord{22388ba6 #13837 A=com.kv.app1 U=0 sz=1}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x30200000 cmp=com.kv.app1/.App1Act1 }
Hist #0: ActivityRecord{1f089f69 u0 com.kv.app1/.App1Act1 t13837}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x30200000 cmp=com.kv.app1/.App1Act1 bnds=[21,86][273,371] }
ProcessRecord{22ad280b 3174:com.kv.app1/u0a1059}
通过以上的实验,相信对allowTaskReparenting有个印象的加深了吧。