前言
本篇只介绍Fragment的生命周期
Activity和Fragment生命周期
二者生命周期对比
image.png
Activity与Fragment生命周期关系
创建过程:
销毁过程:
imageFragment切换时生命周期
取自第二行代码:
碎片的生命周期.png对部分生命周期进行说明:
onAttach:当碎片和活动建立关联的时候调用
onCreateView:为碎片创建视图(加载布局)时调用
onActivityCreated:确保与碎片相关联的活动一定已经创建完毕的时候调用
onDestroyView:当与碎片关联的视图被移除的时候调用
onDetach:当碎片和活动解除关联的时候调用。
举例说明fragment替换时的生命周期执行过程
RightFragment代码(AnotherRightFragment代码与之基本一致)
public class RightFragment extends Fragment {
private static final String TAG = "RightFragment";
public RightFragment() {
}
public static RightFragment newInstance() {
RightFragment fragment = new RightFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate: ");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView: ");
View view = inflater.inflate(R.layout.right_fragment,container,false);
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
Log.d(TAG, "onAttach: ");
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d(TAG, "onActivityCreated: ");
}
@Override
public void onStart() {
super.onStart();
Log.d(TAG, "onStart: ");
}
@Override
public void onResume() {
super.onResume();
Log.d(TAG, "onResume: ");
}
@Override
public void onPause() {
super.onPause();
Log.d(TAG, "onPause: ");
}
@Override
public void onStop() {
super.onStop();
Log.d(TAG, "onStop: ");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.d(TAG, "onDestroyView: ");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy: ");
}
@Override
public void onDetach() {
super.onDetach();
Log.d(TAG, "onDetach: ");
}
}
Main2Activity代码(在布局中有一个FrameLayout来做相应的fragment替换)
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
replaceFragment(new AnotherRightFragment());
}
});
replaceFragment(new RightFragment());
}
private void replaceFragment(Fragment fragment) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.right_layout,fragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
注意代码中在transaction做commit之前的transaction.addToBackStack(null);
启动页面时的打印信息:
05-13 19:12:43.094 25674-25674/com.dhasa.restore D/RightFragment: onAttach:
onCreate:
05-13 19:12:43.095 25674-25674/com.dhasa.restore D/RightFragment: onCreateView:
05-13 19:12:43.099 25674-25674/com.dhasa.restore D/RightFragment: onActivityCreated:
onStart:
05-13 19:12:43.100 25674-25674/com.dhasa.restore D/RightFragment: onResume:
点击按钮,让AnotherRightFragment替换RightFramgent时:
05-13 19:15:05.316 25674-25674/com.dhasa.restore D/AnotherRightFragment: onAttach:
onCreate:
05-13 19:15:05.316 25674-25674/com.dhasa.restore D/RightFragment: onPause:
onStop:
onDestroyView:
05-13 19:15:05.318 25674-25674/com.dhasa.restore D/AnotherRightFragment: onCreateView:
05-13 19:15:05.321 25674-25674/com.dhasa.restore D/AnotherRightFragment: onActivityCreated:
onStart:
onResume:
RightFragment只调用了onPause、onStop和onDestroyView,只是移除了视图,并没有对Fragment进行销毁。
点击安卓手机的back回退键:
05-13 19:17:19.418 25674-25674/com.dhasa.restore D/AnotherRightFragment: onPause:
onStop:
onDestroyView:
05-13 19:17:19.423 25674-25674/com.dhasa.restore D/AnotherRightFragment: onDestroy:
onDetach:
05-13 19:17:19.424 25674-25674/com.dhasa.restore D/RightFragment: onCreateView:
05-13 19:17:19.428 25674-25674/com.dhasa.restore D/RightFragment: onActivityCreated:
onStart:
onResume:
AnotherRightFragment执行了onPause->onStop->onDestroyView->onDestroy->onDetach,然后RightFragment恢复,执行了onCreateView->onActivityCreated->onStart->onResume
现在我们修改Main2Activity中replaceFragment方法中的代码
private void replaceFragment(Fragment fragment) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.right_layout,fragment);
//transaction.addToBackStack(null);
transaction.commit();
}
将transaction.addToBackStack(null);代码注释掉
其他打印输出一致,在用AnotherRightFragment替换RightFragment时打印如下:
05-13 19:23:44.779 30558-30558/com.dhasa.restore D/AnotherRightFragment: onAttach:
onCreate:
05-13 19:23:44.780 30558-30558/com.dhasa.restore D/RightFragment: onPause:
onStop:
onDestroyView:
05-13 19:23:44.781 30558-30558/com.dhasa.restore D/RightFragment: onDestroy:
onDetach:
05-13 19:23:44.782 30558-30558/com.dhasa.restore D/AnotherRightFragment: onCreateView:
05-13 19:23:44.785 30558-30558/com.dhasa.restore D/AnotherRightFragment: onActivityCreated:
onStart:
onResume:
RightFragment调用了onPause->onStop->onDestroyView->onDestroy->onDetach,对Fragment进行销毁并与Activity分离。此时按返回键,该Activity页面直接finish返回。
网友评论