这里首先推荐大家一本Android的学习书籍:《第一行代码》,这本书还是写得非常好的。好了,进入正题,我们先来了解一下Activity、Fragment的的生命周期中经历的几个阶段。
Activity | Fragment |
---|---|
下面我们假设有如下的场景,Activity内部有一个Fragment,Fragment内部是一个SurfaceView。
分为两种情况,一种是在资源文件直接加载Fragment标签,例如:
<Fragment
android:name="com.myapplication.MainFragment"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
另一种是动态加载Fragment,先在资源文件定义存放Fragment的容器,然后通过FragmentTransaction:add()
和FragmentTransaction:commit()
函数创建并加载,例如:
<FrameLayout
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
测试代码如下:
Activity
public class MainActivity extends AppCompatActivity implements {
private MainFragment mMainFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
LogUtils.init(0, ILog.LEVEL_INFO, "magnity", null);
LogUtils.i("activity onCreate enter");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMainFragment = (MainFragment)getSupportFragmentManager().
findFragmentById(R.id.main_content);
if (mMainFragment == null) {
mMainFragment = MainFragment.newInstance();
getSupportFragmentManager().beginTransaction().
add(R.id.main_content, mMainFragment).commit();
}
LogUtils.i("activity onCreate leave");
}
@Override
protected void onStart() {
LogUtils.i("activity onStart enter");
super.onStart();
LogUtils.i("activity onStart leave");
}
@Override
protected void onResume() {
LogUtils.i("activity onResume enter");
super.onResume();
LogUtils.i("activity onResume leave");
}
@Override
protected void onPause() {
LogUtils.i("activity onPause enter");
super.onPause();
LogUtils.i("activity onPause leave");
}
@Override
protected void onStop() {
LogUtils.i("activity onStop enter");
super.onStop();
LogUtils.i("activity onStop leave");
}
@Override
protected void onDestroy() {
LogUtils.i("activity onDestroy enter");
super.onDestroy();
LogUtils.i("activity onDestroy leave");
}
}
Fragment
public class MainFragment extends Fragment {
public MainFragment() {
// Required empty public constructor
}
public static MainFragment newInstance(String param1, String param2) {
MainFragment fragment = new MainFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
LogUtils.i("fragment onCreate enter");
super.onCreate(savedInstanceState);
LogUtils.i("fragment onCreate leave");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LogUtils.i("fragment onCreateView enter");
View v = inflater.inflate(R.layout.fragment_main, container, false);
LogUtils.i("fragment onCreateView leave");
return v;
}
@Override
public void onAttach(Context context) {
LogUtils.i("fragment onAttach enter");
super.onAttach(context);
LogUtils.i("fragment onAttach leave");
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
LogUtils.i("fragment onActivityCreated enter");
super.onActivityCreated(savedInstanceState);
LogUtils.i("fragment onActivityCreated leave");
}
@Override
public void onStart() {
LogUtils.i("fragment onStart enter");
super.onStart();
LogUtils.i("fragment onStart leave");
}
@Override
public void onResume() {
LogUtils.i("fragment onResume enter");
super.onResume();
LogUtils.i("fragment onResume leave");
}
@Override
public void onPause() {
LogUtils.i("fragment onPause enter");
super.onPause();
LogUtils.i("fragment onPause leave");
}
@Override
public void onStop() {
LogUtils.i("fragment onStop enter");
super.onStop();
LogUtils.i("fragment onStop leave");
}
@Override
public void onDestroyView() {
LogUtils.i("fragment onDestroyView enter");
super.onDestroyView();
LogUtils.i("fragment onDestroyView leave");
}
@Override
public void onDestroy() {
LogUtils.i("fragment onDestroy enter");
super.onDestroy();
LogUtils.i("fragment onDestroy leave");
}
@Override
public void onDetach() {
LogUtils.i("fragment onDetach enter");
super.onDetach();
mListener = null;
LogUtils.i("fragment onDetach leave");
}
}
SurfaceView
public class MainSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
public MainSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
getHolder().addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
LogUtils.i("surface surfaceCreated");
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
LogUtils.i("surface surfaceChanged");
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
LogUtils.i("surface surfaceDestroyed");
}
}
打印结果
- 动态加载
启动应用并加载MainActivity,打印如下:
activity onCreate enter
activity onCreate leave
activity onStart enter
fragment onAttach enter
fragment onAttach leave
fragment onCreate enter
fragment onCreate leave
fragment onCreateView enter
fragment onCreateView leave
fragment onActivityCreated enter
fragment onActivityCreated leave
fragment onStart enter
fragment onStart leave
activity onStart leave
activity onResume enter
activity onResume leave
fragment onResume enter
fragment onResume leave
surface surfaceCreated
surface surfaceChanged
按HOME键切到后台,打印如下:
activity onPause enter
fragment onPause enter
fragment onPause leave
activity onPause leave
surface surfaceDestroyed
activity onStop enter
fragment onStop enter
fragment onStop leave
activity onStop leave
恢复到前台,打印如下:
activity onStart enter
fragment onStart enter
fragment onStart leave
activity onStart leave
activity onResume enter
activity onResume leave
fragment onResume enter
fragment onResume leave
surface surfaceCreated
surface surfaceChanged
退出MainActivity,打印如下:
activity onPause enter
fragment onPause enter
fragment onPause leave
activity onPause leave
surface surfaceDestroyed
activity onStop enter
fragment onStop enter
fragment onStop leave
activity onStop leave
activity onDestroy enter
fragment onDestroyView enter
fragment onDestroyView leave
fragment onDestroy enter
fragment onDestroy leave
fragment onDetach enter
fragment onDetach leave
activity onDestroy leave
- 静态加载
启动应用并加载MainActivity,打印如下:
activity onCreate enter
fragment onAttach enter
fragment onAttach leave
fragment onCreate enter
fragment onCreate leave
fragment onCreateView enter
fragment onCreateView leave
activity onCreate leave
activity onStart enter
fragment onActivityCreated enter
fragment onActivityCreated leave
fragment onStart enter
fragment onStart leave
activity onStart leave
activity onResume enter
activity onResume leave
fragment onResume enter
fragment onResume leave
surface surfaceCreated
surface surfaceChanged
按HOME键切到后台,打印如下:
activity onPause enter
fragment onPause enter
fragment onPause leave
activity onPause leave
surface surfaceDestroyed
activity onStop enter
fragment onStop enter
fragment onStop leave
activity onStop leave
恢复到前台,打印如下:
activity onStart enter
fragment onStart enter
fragment onStart leave
activity onStart leave
activity onResume enter
activity onResume leave
fragment onResume enter
fragment onResume leave
surface surfaceCreated
surface surfaceChanged
退出MainActivity,打印如下:
activity onPause enter
fragment onPause enter
fragment onPause leave
activity onPause leave
surface surfaceDestroyed
activity onStop enter
fragment onStop enter
fragment onStop leave
activity onStop leave
activity onDestroy enter
fragment onDestroyView enter
fragment onDestroyView leave
fragment onDestroy enter
fragment onDestroy leave
fragment onDetach enter
fragment onDetach leave
activity onDestroy leave
总结
可以看到Activity在动态加载和静态加载时,Fragment的回调函数的调用顺序略有差别,但也有规律可循,而在Activity退出时其调用顺序则完全一致。了解这些回调函数的调用顺序有助于我们开发更具健壮性的程序。
网友评论