Fragment生命周期
Fragment 生命周期状态
@Override public void onAttach(@NonNull Context context) { super.onAttach(context); // 与 Activity 进行交互 }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 初始化代码 }
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_layout, container, false); }
@Override public void onViewCreated(@NonNull View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); // 初始化视图组件 }
@Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); // 访问 Activity 的 UI 组件 }
@Override public void onStart() { super.onStart(); // 注册监听器或更新 UI }
@Override public void onResume() { super.onResume(); // 恢复活动或获取用户输入 }
@Override public void onPause() { super.onPause(); // 暂停活动 }
@Override public void onStop() { super.onStop(); // 保存数据或释放资源 }
@Override public void onDestroyView() { super.onDestroyView(); // 清理视图资源 }
@Override public void onDestroy() { super.onDestroy(); // 清理资源 }
@Override public void onDetach() { super.onDetach(); // 与 Activity 的交互结束 }
生命周期总结
小结
Last updated