Activity、Fragment生命周期以及横竖屏切换的生命周期

        Fragment是在Android 3.0 (API level 11)开始引入的,它能让你的app在现有基础上性能大幅度提高,并且占用内存降低,同样的界面Activity占用内存比Fragment要多,响应速度Fragment比Activty在中低端手机上快了很多,甚至能达到好几倍,"单Activity + 多Fragment架构"和"多模块Activity + 多Fragment架构"应运而生!

       Fragment必须嵌入在一个activity中,同时fraFgment的生命周期受 activity而影响。Activity的生命周期有7个函数,Fragment的生命周期有11个函数。

  

Activity、Fragment生命周期以及横竖屏切换的生命周期_第1张图片

Activity生命周期除上述6个方法还有一个Restart()方法,该方法在该Activity从不可见(仍存在)到重新可见时调用。

Activity、Fragment生命周期以及横竖屏切换的生命周期_第2张图片

下面是测试代码(布局文件比较简单就不贴上来了):

//主界面Activity
public class MainActivity extends AppCompatActivity  {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("LIFECYCLE_TEST","Activity:onCreate");
        setContentView(R.layout.activity_main);
        TestFragment testFragment = new TestFragment();
        getSupportFragmentManager().beginTransaction().replace(R.id.fl,testFragment).commit();
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e("LIFECYCLE_TEST","Activity:onStart");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.e("LIFECYCLE_TEST","Activity:onRestart");
    }

    @Override
    protected void onResume() {
        super.onResume(); 
        Log.e("LIFECYCLE_TEST","Activity;onResume");
    }

    @Override
    protected void onPause() {
        super.onPause(); 
        Log.e("LIFECYCLE_TEST","Activityz;onPause");
    }

    @Override
    protected void onStop() {
        super.onStop(); 
        Log.e("LIFECYCLE_TEST","Activity:onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy(); 
        Log.e("LIFECYCLE_TEST","Activity:"+"onDestroy");
    }

}
//嵌套MainActivity中的Fragment
public class TestFragment extends Fragment {

    public TestFragment() {
        // Required empty public constructor
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Log.w("LIFECYCLE_TEST", "Fragment:onAttach");

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        Log.w("LIFECYCLE_TEST", "Fragment:onAttachActivity");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.w("LIFECYCLE_TEST", "Fragment:onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        Log.w("LIFECYCLE_TEST", "Fragment:onCreateView");
      View view =  inflater.inflate(R.layout.fragment_test, container, false);
      initView(view);
        return view;
    }

    private void initView(View view) {
        view.findViewById(R.id.open_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),TestActivity.class);
                startActivity(intent);
            }
        });
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.w("LIFECYCLE_TEST", "Fragment:onAttach");
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.w("LIFECYCLE_TEST", "Fragment:onStart");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.w("LIFECYCLE_TEST", "Fragment:onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.w("LIFECYCLE_TEST", "Fragment:onPause");
    }


    @Override
    public void onStop() {
        super.onStop();
        Log.w("LIFECYCLE_TEST", "Fragment:onStop");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.w("LIFECYCLE_TEST", "Fragment:onDestroyView");
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.w("LIFECYCLE_TEST", "Fragment:onDestroy");
    }


    @Override
    public void onDetach() {
        super.onDetach();
        Log.w("LIFECYCLE_TEST", "Fragment:onDetach");

    }

}
//从TestFragment中打开的Activity
public class TestActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
    }
}

测试结果如下:

1、运行程序

 Activity、Fragment生命周期以及横竖屏切换的生命周期_第3张图片

2、打开另一个Activity

Activity、Fragment生命周期以及横竖屏切换的生命周期_第4张图片

3、关闭刚打开的Activity

Activity、Fragment生命周期以及横竖屏切换的生命周期_第5张图片

4、App退到后台

5、从后台重新打开App

Activity、Fragment生命周期以及横竖屏切换的生命周期_第6张图片

6、按back键退出

Activity、Fragment生命周期以及横竖屏切换的生命周期_第7张图片

       从上面测试结果可以看出,由于Fragment依赖Activity而存在,故在创建时,Acvitity的生命周期方法均先于Fragment执行,销毁、打卡新的Activity、App退到后台时候,Fragment生命周期方法先于Activity执行。当从后台重新打开App或者关闭最上面的Activity回到主界面,主界面Activity会执行onRestart(),生命周期方法与其他情况不同,具体看图。

7、横竖屏切换时Activity和Fragment生命周期执行顺序

Activity、Fragment生命周期以及横竖屏切换的生命周期_第8张图片

可以看出,当横竖屏切换时,Activity和Fragment都会先销毁再重建。

8、当在AndroidManifest中配置Activity为 android:configChanges="orientation|screenSize"时,只会走Activity和Fragment的onConfigurationChanged方法,且Fragment先于Activity执行。

 

 

 

 

你可能感兴趣的:(android)