2-AVI--Fragment生命周期测试

作者: e4e52c116681 | 来源:发表于2018-08-28 14:56 被阅读3次

    零、前言

    [1].两个Fragment,点击左右按钮切换相应的Fragment
    [2].观察Activity 和两个Fragment生命周期变化
    [3].布局很简单,挺多的,就不贴了,会给图

    具体情况如图:
    Fragment生命周期.png
    布局:
    fragment生命周期布局.gif
    LifeOfFgActivity
    public class LifeOfFgActivity extends AppCompatActivity {
        private static final String TAG = "LifeOfFgActivity";
        @BindView(R.id.btn_left)
        Button mTvTitle1;
        @BindView(R.id.btn_right)
        Button mTvTitle2;
        @BindView(R.id.ll_life_root)
        LinearLayout mLlLifeRoot;
        @BindView(R.id.fl_life_root)
        FrameLayout mFlLifeRoot;
        private FragmentManager mFm;
        private FragmentTransaction mFt;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_life);
            Log.e(TAG, "onCreate: &&&&&&LifeOfFgActivity");
            ButterKnife.bind(this);
            initFragment();//初始Fragment
        }
    
        @Override
        protected void onStart() {
            super.onStart();
            Log.e(TAG, "onStart: &&&&&&LifeOfFgActivity");
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            Log.e(TAG, "onResume: &&&&&&LifeOfFgActivity");
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            Log.e(TAG, "onPause: &&&&&&LifeOfFgActivity");
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            Log.e(TAG, "onStop: &&&&&&LifeOfFgActivity");
        }
    
        @Override
        protected void onRestart() {
            super.onRestart();
            Log.e(TAG, "onRestart: &&&&&&LifeOfFgActivity");
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            Log.e(TAG, "onDestroy: &&&&&&LifeOfFgActivity");
        }
    
        private void initFragment() {
            mFm = getFragmentManager();//1.获取FragmentManager
            mFt = mFm.beginTransaction();//2.fm开启事务
            mFt.add(R.id.fl_life_root, new Fragment1()); //3.动态添加
            mFt.commit();//4.提交事务
        }
    
        @OnClick({R.id.btn_left, R.id.btn_right})
        public void onViewClicked(View view) {
            mFt = mFm.beginTransaction();//2.fm开启事务
            switch (view.getId()) {
                case R.id.btn_left:
                    mFlLifeRoot.removeAllViews();//先清屏
                    mFt.replace(R.id.fl_life_root, new Fragment1()); //3.动态添加
                    break;
                case R.id.btn_right:
                    mFlLifeRoot.removeAllViews();//先清屏
                    mFt.replace(R.id.fl_life_root, new Fragment2()); //3.动态添加
                    break;
            }
            mFt.commit();//4.提交事务
        }
    }
    
    Fragment1
    public class Fragment1 extends Fragment {
        private static final String TAG = "Fragment1";
        
    
        @Override
        public void onAttach(Context context) {
            super.onAttach(context);
            Log.e(TAG, "onAttach: ====Fragment1");
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Log.e(TAG, "onCreate: ====Fragment1");
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            Log.e(TAG, "onCreateView:====Fragment1 ");
            return inflater.inflate(R.layout.fragment_fragment1, container, false);
        }
    
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            Log.e(TAG, "onActivityCreated:====Fragment1 ");
        }
    
        @Override
        public void onStart() {
            super.onStart();
            Log.e(TAG, "onStart: ====Fragment1");
        }
    
        @Override
        public void onResume() {
            super.onResume();
            Log.e(TAG, "onResume:====Fragment1 ");
        }
    
        @Override
        public void onPause() {
            super.onPause();
            Log.e(TAG, "onPause:====Fragment1 ");
        }
    
        @Override
        public void onStop() {
            super.onStop();
            Log.e(TAG, "onStop: ====Fragment1");
        }
    
        @Override
        public void onDestroyView() {
            super.onDestroyView();
            Log.e(TAG, "onDestroyView: ====Fragment1");
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            Log.e(TAG, "onDestroy:====Fragment1 ");
        }
    
        @Override
        public void onDetach() {
            super.onDetach();
            Log.e(TAG, "onDetach:====Fragment1 ");
        }
    }
    
    Fragment2
    public class Fragment2 extends Fragment {
        private static final String TAG = "Fragment2";
    
    
        @Override
        public void onAttach(Context context) {
            super.onAttach(context);
            Log.e(TAG, "onAttach: >>>>>Fragment2");
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Log.e(TAG, "onCreate: >>>>>Fragment2");
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            Log.e(TAG, "onCreateView:>>>>>Fragment2 ");
            return inflater.inflate(R.layout.fragment_fragment2, container, false);
        }
    
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            Log.e(TAG, "onActivityCreated:>>>>>Fragment2 ");
        }
    
        @Override
        public void onStart() {
            super.onStart();
            Log.e(TAG, "onStart: >>>>>Fragment2");
        }
    
        @Override
        public void onResume() {
            super.onResume();
            Log.e(TAG, "onResume:>>>>>Fragment2 ");
        }
    
        @Override
        public void onPause() {
            super.onPause();
            Log.e(TAG, "onPause:>>>>>Fragment2 ");
        }
    
        @Override
        public void onStop() {
            super.onStop();
            Log.e(TAG, "onStop: >>>>>Fragment2");
        }
    
        @Override
        public void onDestroyView() {
            super.onDestroyView();
            Log.e(TAG, "onDestroyView: >>>>>Fragment2");
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            Log.e(TAG, "onDestroy:>>>>>Fragment2 ");
        }
    
        @Override
        public void onDetach() {
            super.onDetach();
            Log.e(TAG, "onDetach:>>>>>Fragment2 ");
        }
    }
    
    

    本文由张风捷特烈原创,转载请注明
    更多安卓技术欢迎访问:https://www.jianshu.com/c/004f3fe34c94
    张风捷特烈个人网站,编程笔记请访问:http://www.toly1994.com
    你的喜欢与支持将是我最大的动力

    相关文章

      网友评论

        本文标题:2-AVI--Fragment生命周期测试

        本文链接:https://www.haomeiwen.com/subject/cnzfwftx.html