public class MainActivity extends Activity implements View.OnClickListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViewById(R.id.btn).setOnClickListener(this); FragmentManager.enableDebugLogging(true); } @Override public void onClick(View view) { showDialog(); } private void showDialog() { new MyDialogFragment().show(getFragmentManager(), "Tag1"); } }
08-01 21:23:07.521 16125-16125/com.androidbook.fragments.dialogs V/BackStackEntry﹕ Commit: android.app.BackStackRecord@41cf3ea0
08-01 21:23:07.521 16125-16125/com.androidbook.fragments.dialogs V/BackStackEntry﹕ Run: android.app.BackStackRecord@41cf3ea0
08-01 21:23:07.521 16125-16125/com.androidbook.fragments.dialogs V/FragmentManager﹕ add: MyDialogFragment{41cf3dd8 Tag1}
08-01 21:23:07.521 16125-16125/com.androidbook.fragments.dialogs V/FragmentManager﹕ Allocated fragment index MyDialogFragment{41cf3dd8 #0 Tag1}
08-01 21:23:07.521 16125-16125/com.androidbook.fragments.dialogs V/FragmentManager﹕ moveto CREATED: MyDialogFragment{41cf3dd8 #0 Tag1}
08-01 21:23:07.526 16125-16125/com.androidbook.fragments.dialogs V/FragmentManager﹕ moveto ACTIVITY_CREATED: MyDialogFragment{41cf3dd8 #0 Tag1}
08-01 21:23:07.531 16125-16125/com.androidbook.fragments.dialogs V/FragmentManager﹕ moveto STARTED: MyDialogFragment{41cf3dd8 #0 Tag1}
08-01 21:23:07.541 16125-16125/com.androidbook.fragments.dialogs V/FragmentManager﹕ moveto RESUMED: MyDialogFragment{41cf3dd8 #0 Tag1}
这是显示 DF(DialogFragment) 时的日志。
提交事务,实质上是提交一条 BSR(BackStackRecord),然后处理这条 BSR。
FM(FragmentManger) 开始 add DF 41cf3dd8(唯一标识) Tag1。
为该 DF 申请空间,然后经历了 DF 的4中生命周期。
08-01 21:31:56.551 16125-16125/com.androidbook.fragments.dialogs V/BackStackEntry﹕ Commit: android.app.BackStackRecord@41cff170
08-01 21:31:56.566 16125-16125/com.androidbook.fragments.dialogs V/BackStackEntry﹕ Run: android.app.BackStackRecord@41cff170
08-01 21:31:56.566 16125-16125/com.androidbook.fragments.dialogs V/FragmentManager﹕ remove: MyDialogFragment{41cf3dd8 #0 Tag1} nesting=0
08-01 21:31:56.566 16125-16125/com.androidbook.fragments.dialogs V/FragmentManager﹕ movefrom RESUMED: MyDialogFragment{41cf3dd8 #0 Tag1}
08-01 21:31:56.571 16125-16125/com.androidbook.fragments.dialogs V/FragmentManager﹕ movefrom STARTED: MyDialogFragment{41cf3dd8 #0 Tag1}
08-01 21:31:56.571 16125-16125/com.androidbook.fragments.dialogs V/FragmentManager﹕ movefrom ACTIVITY_CREATED: MyDialogFragment{41cf3dd8 #0 Tag1}
08-01 21:31:56.571 16125-16125/com.androidbook.fragments.dialogs V/FragmentManager﹕ movefrom CREATED: MyDialogFragment{41cf3dd8 #0 Tag1}
08-01 21:31:56.571 16125-16125/com.androidbook.fragments.dialogs V/FragmentManager﹕ Freeing fragment index MyDialogFragment{41cf3dd8 #0 Tag1}
这是 dismiss DF 时产生的日志,dismiss 和 hide 不同,是确实会释放资源的。
同样的,提交BSR,执行BSR,
FM 开始 remove DF 41cf3dd8 #0(位置) Tag1 nesting=0(没有关联,这个不管,我不用addBackStack的),
与之前反向的顺序经历四个生命周期,最后释放资源。
这样的DF已经满足了基本的提示性质的 Dialog 了。
但对于遮罩Dialog 或那些有性能洁癖的人,应该尝试下面这种。
public class MainActivity extends Activity implements View.OnClickListener { private DialogFragment dialogFragment = new DialogFragment(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViewById(R.id.btn).setOnClickListener(this); FragmentManager.enableDebugLogging(true); } @Override public void onClick(View view) { dialogFragment.show(getFragmentManager(), "Tag1"); dialogFragment.dismiss(); } }
先是提交两条 BSR,可以看到不是提交后就立即执行的,而是处理 Message 队列那样。
之后的过程和前面是一样的,也许根本就没节约多少开销。
@Override public void onClick(View view) { dialogFragment.show(getFragmentManager(), "Tag1"); dialogFragment.show(getFragmentManager(), "Tag1"); }
提交两条 BSR, FM add DF 41cbdf88 Tag1 ----- add DF 41cbdf88 #0 Tag1
显示效果方面已经崩了,可以发现 41dbdf88 是一样的,也许就是地址或地址的映射。
这告诉我们,不要 add 已经 add 过的 DF。
@Override public void onClick(View view) { dialogFragment.show(getFragmentManager(), "Tag1"); dialogFragment.show(getFragmentManager(), "Tag2"); }
这下抛异常了,这是我唯一见过会抛异常的可能。
就是说地址和 Tag 是唯一绑定的。
@Override public void onClick(View view) { new DialogFragment().show(getFragmentManager(),"Tag1"); new DialogFragment().show(getFragmentManager(),"Tag1"); }
08-01 21:52:50.556 20073-20073/com.androidbook.fragments.dialogs V/BackStackEntry﹕ Commit: android.app.BackStackRecord@41cdb2c0
08-01 21:52:50.556 20073-20073/com.androidbook.fragments.dialogs V/BackStackEntry﹕ Commit: android.app.BackStackRecord@41cdb778
08-01 21:52:50.556 20073-20073/com.androidbook.fragments.dialogs V/BackStackEntry﹕ Run: android.app.BackStackRecord@41cdb2c0
08-01 21:52:50.561 20073-20073/com.androidbook.fragments.dialogs V/FragmentManager﹕ add: DialogFragment{41cdb1b8 Tag1}
08-01 21:52:50.561 20073-20073/com.androidbook.fragments.dialogs V/FragmentManager﹕ Allocated fragment index DialogFragment{41cdb1b8 #0 Tag1}
08-01 21:52:50.561 20073-20073/com.androidbook.fragments.dialogs V/FragmentManager﹕ moveto CREATED: DialogFragment{41cdb1b8 #0 Tag1}
08-01 21:52:50.561 20073-20073/com.androidbook.fragments.dialogs V/FragmentManager﹕ moveto ACTIVITY_CREATED: DialogFragment{41cdb1b8 #0 Tag1}
08-01 21:52:50.561 20073-20073/com.androidbook.fragments.dialogs V/FragmentManager﹕ moveto STARTED: DialogFragment{41cdb1b8 #0 Tag1}
08-01 21:52:50.571 20073-20073/com.androidbook.fragments.dialogs V/FragmentManager﹕ moveto RESUMED: DialogFragment{41cdb1b8 #0 Tag1}
08-01 21:52:50.571 20073-20073/com.androidbook.fragments.dialogs V/BackStackEntry﹕ Run: android.app.BackStackRecord@41cdb778
08-01 21:52:50.571 20073-20073/com.androidbook.fragments.dialogs V/FragmentManager﹕ add: DialogFragment{41cdb6b0 Tag1}
08-01 21:52:50.571 20073-20073/com.androidbook.fragments.dialogs V/FragmentManager﹕ Allocated fragment index DialogFragment{41cdb6b0 #1 Tag1}
08-01 21:52:50.571 20073-20073/com.androidbook.fragments.dialogs V/FragmentManager﹕ moveto CREATED: DialogFragment{41cdb6b0 #1 Tag1}
08-01 21:52:50.571 20073-20073/com.androidbook.fragments.dialogs V/FragmentManager﹕ moveto ACTIVITY_CREATED: DialogFragment{41cdb6b0 #1 Tag1}
08-01 21:52:50.571 20073-20073/com.androidbook.fragments.dialogs V/FragmentManager﹕ moveto STARTED: DialogFragment{41cdb6b0 #1 Tag1}
08-01 21:52:50.581 20073-20073/com.androidbook.fragments.dialogs V/FragmentManager﹕ moveto RESUMED: DialogFragment{41cdb6b0 #1 Tag1}
他们的 Tag 是一样的,但地址不同。
那么如果调用 findFragmentByTag 会取到哪个呢?
事实上会拿到后添加的那个,并不会抛出异常。
所以,要注意的就是不要改变的 DF 的 Tag。