在基于上一篇关于Activity的生命周期的深入了解之后,我想大家应该开始意识到一个问题就是,虽然有些类,接口,方法平时经常使用,但是只是了解一个大概,当在面试的时候考官一深入去体察你的知识深度,你就hold不住了。那么在这篇文章中,我们开始针对Fragment的生命周期进行一些知识挖掘,我依然会带大家通过阅读源码的方式了解Fragment。
07-17 08:41:51.441 9232-9232/com.kanzhun.lifecycle D/FragmentA: onAttach
07-17 08:41:51.441 9232-9232/com.kanzhun.lifecycle D/FragmentA: onCreate
07-17 08:41:51.451 9232-9232/com.kanzhun.lifecycle D/FragmentA: onCreateView
07-17 08:41:51.461 9232-9232/com.kanzhun.lifecycle D/FragmentA: onViewCreated
07-17 08:41:51.461 9232-9232/com.kanzhun.lifecycle D/FragmentA: onActivityCreated
07-17 08:41:51.461 9232-9232/com.kanzhun.lifecycle D/FragmentA: onStart
07-17 08:41:51.461 9232-9232/com.kanzhun.lifecycle D/FragmentA: onResume
(2)Fragment进入后台或者锁屏时:
07-17 08:44:28.871 9232-9232/com.kanzhun.lifecycle D/FragmentA: onPause
07-17 08:44:29.241 9232-9232/com.kanzhun.lifecycle D/FragmentA: onStop
(3)Fragment从后台返回前台或者点亮屏幕时:
07-17 08:45:42.711 9232-9232/com.kanzhun.lifecycle D/FragmentA: onStart
07-17 08:45:42.711 9232-9232/com.kanzhun.lifecycle D/FragmentA: onResume
(4)销毁Fragment的时候:
07-17 08:46:40.291 9232-9232/com.kanzhun.lifecycle D/FragmentA: onPause
07-17 08:46:40.631 9232-9232/com.kanzhun.lifecycle D/FragmentA: onStop
07-17 08:46:40.631 9232-9232/com.kanzhun.lifecycle D/FragmentA: onDestroyView
07-17 08:46:40.631 9232-9232/com.kanzhun.lifecycle D/FragmentA: onDestroy
07-17 08:46:40.631 9232-9232/com.kanzhun.lifecycle D/FragmentA: onDetach
由此来看,我们通过日志的打印又重新对于Fragment的生命周期进行了一次简单的回顾。
public class FragmentLifecycleActivity extends FragmentActivity implements View.OnClickListener {
private FragmentManager fm;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_lifecycle);
findViewById(R.id.btn_fragmentA).setOnClickListener(this);
findViewById(R.id.btn_fragmentB).setOnClickListener(this);
fm = getSupportFragmentManager();
}
@Override
public void onClick(View view) {
FragmentTransaction ft = fm.beginTransaction();
switch (view.getId()) {
case R.id.btn_fragmentA:
ft.replace(R.id.ll_container, FragmentA.getInstance(null));
ft.addToBackStack(null);
ft.commit();
break;
case R.id.btn_fragmentB:
ft.replace(R.id.ll_container, FragmentB.getInstance(null));
ft.addToBackStack(null);
ft.commit();
break;
default:
break;
}
}
}
在以上的代码中,我采取replace()的方式替换之前所存在的fragment,并且每次替换Fragment时都添加addToBackStack()方法,我们看看他对于生命周期有什么影响:
07-17 09:03:38.441 26271-26271/com.kanzhun.lifecycle D/FragmentA: onAttach
07-17 09:03:38.441 26271-26271/com.kanzhun.lifecycle D/FragmentA: onCreate
07-17 09:03:38.441 26271-26271/com.kanzhun.lifecycle D/FragmentA: onCreateView
07-17 09:03:38.441 26271-26271/com.kanzhun.lifecycle D/FragmentA: onViewCreated
07-17 09:03:38.441 26271-26271/com.kanzhun.lifecycle D/FragmentA: onActivityCreated
07-17 09:03:38.441 26271-26271/com.kanzhun.lifecycle D/FragmentA: onStart
07-17 09:03:38.441 26271-26271/com.kanzhun.lifecycle D/FragmentA: onResume
(2)这时,点击fragmentB的按钮让FragmentB去替换FragmentA:
07-17 09:08:24.531 26271-26271/com.kanzhun.lifecycle D/FragmentB: onAttach
07-17 09:08:24.531 26271-26271/com.kanzhun.lifecycle D/FragmentB: onCreate
07-17 09:08:24.531 26271-26271/com.kanzhun.lifecycle D/FragmentA: onPause
07-17 09:08:24.531 26271-26271/com.kanzhun.lifecycle D/FragmentA: onStop
07-17 09:08:24.531 26271-26271/com.kanzhun.lifecycle D/FragmentA: onDestroyView
07-17 09:08:24.531 26271-26271/com.kanzhun.lifecycle D/FragmentB: onCreateView
07-17 09:08:24.531 26271-26271/com.kanzhun.lifecycle D/FragmentB: onViewCreated
07-17 09:08:24.531 26271-26271/com.kanzhun.lifecycle D/FragmentB: onActivityCreated
07-17 09:08:24.531 26271-26271/com.kanzhun.lifecycle D/FragmentB: onStart
07-17 09:08:24.531 26271-26271/com.kanzhun.lifecycle D/FragmentB: onResume
在FragmentB启动之后,我们发现FragmentA虽然被替换并且运行了onDestroyView()方法,但是并没有运行onDestroy()和onDetach()方法。
07-17 09:14:12.221 26271-26271/com.kanzhun.lifecycle D/FragmentB: onPause
07-17 09:14:12.221 26271-26271/com.kanzhun.lifecycle D/FragmentB: onStop
07-17 09:14:12.221 26271-26271/com.kanzhun.lifecycle D/FragmentB: onDestroyView
07-17 09:14:12.221 26271-26271/com.kanzhun.lifecycle D/FragmentB: onDestroy
07-17 09:14:12.221 26271-26271/com.kanzhun.lifecycle D/FragmentB: onDetach
07-17 09:14:12.231 26271-26271/com.kanzhun.lifecycle D/FragmentA: onCreateView
07-17 09:14:12.231 26271-26271/com.kanzhun.lifecycle D/FragmentA: onViewCreated
07-17 09:14:12.231 26271-26271/com.kanzhun.lifecycle D/FragmentA: onActivityCreated
07-17 09:14:12.231 26271-26271/com.kanzhun.lifecycle D/FragmentA: onStart
07-17 09:14:12.231 26271-26271/com.kanzhun.lifecycle D/FragmentA: onResume
FragmentA被重新展现出来,我们发现此时FragmentB由于调用了onDestroy()和onDetach()方法已经完全被抛弃。FragmentA由于在FragmentManager的栈中依然存在,所以不需要调用onAttach()和onCreate()方法。
/**
* Add this transaction to the back stack. This means that the transaction
* will be remembered after it is committed, and will reverse its operation
* when later popped off the stack.
*
* @param name An optional name for this back stack state, or null.
*/
public abstract FragmentTransaction addToBackStack(@Nullable String name);
@Override
public void onClick(View view) {
FragmentTransaction ft = fm.beginTransaction();
switch (view.getId()) {
case R.id.btn_fragmentA:
hideAllFragments(ft);
if (fragmentA == null) {
fragmentA = FragmentA.getInstance(null);
ft.add(R.id.ll_container, fragmentA);
ft.addToBackStack(null);
} else {
ft.show(fragmentA);
}
ft.commit();
break;
case R.id.btn_fragmentB:
hideAllFragments(ft);
if (fragmentB == null) {
fragmentB = FragmentB.getInstance(null);
ft.add(R.id.ll_container, fragmentB);
ft.addToBackStack(null);
} else {
ft.show(fragmentB);
}
ft.commit();
break;
default:
break;
}
}
如果Fragment已经被加入到FragmentManager了,我们需要在该fragment显示之前,先隐藏所有的Fragment。
private void hideAllFragments(FragmentTransaction ft) {
List fragments = fm.getFragments();
if (fragments == null || fragments.size() <= 0) return;
for (Fragment f : fragments) {
if (f == null) continue;
ft.hide(f);
}
}
07-17 18:47:58.179 16467-16467/com.kanzhun.lifecycle D/FragmentA: onAttach
07-17 18:47:58.189 16467-16467/com.kanzhun.lifecycle D/FragmentA: onCreate
07-17 18:47:58.189 16467-16467/com.kanzhun.lifecycle D/FragmentA: onCreateView
07-17 18:47:58.189 16467-16467/com.kanzhun.lifecycle D/FragmentA: onViewCreated
07-17 18:47:58.189 16467-16467/com.kanzhun.lifecycle D/FragmentA: onActivityCreated
07-17 18:47:58.189 16467-16467/com.kanzhun.lifecycle D/FragmentA: onStart
07-17 18:47:58.189 16467-16467/com.kanzhun.lifecycle D/FragmentA: onResume
(2)点击FragmentB再将FragmentB添加到FragmentManager时,FragmentB也会调用自己的生命周期如下方法。
07-17 18:49:37.169 16467-16467/com.kanzhun.lifecycle D/FragmentB: onAttach
07-17 18:49:37.169 16467-16467/com.kanzhun.lifecycle D/FragmentB: onCreate
07-17 18:49:37.169 16467-16467/com.kanzhun.lifecycle D/FragmentB: onCreateView
07-17 18:49:37.169 16467-16467/com.kanzhun.lifecycle D/FragmentB: onViewCreated
07-17 18:49:37.169 16467-16467/com.kanzhun.lifecycle D/FragmentB: onActivityCreated
07-17 18:49:37.169 16467-16467/com.kanzhun.lifecycle D/FragmentB: onStart
07-17 18:49:37.169 16467-16467/com.kanzhun.lifecycle D/FragmentB: onResume
(3)再次点击A、B之后,则不再有生命周期方法的调用。