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


    零、前言

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

    具体情况如图:
    9414344-f4ff4ef647ab6ae7.png
    Fragment生命周期.png
    布局:
    9414344-ef995d51dfb9580b.gif
    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 ");
        }
    }
    
    

    后记、

    1.声明:

    [1]本文由张风捷特烈原创,转载请注明
    [2]欢迎广大编程爱好者共同交流
    [3]个人能力有限,如有不正之处欢迎大家批评指证,必定虚心改正
    [4]你的喜欢与支持将是我最大的动力

    2.连接传送门:

    更多安卓技术欢迎访问:安卓技术栈
    我的github地址:欢迎star
    简书首发,腾讯云+社区同步更新
    张风捷特烈个人网站,编程笔记请访问:http://www.toly1994.com

    3.联系我

    QQ:1981462002
    邮箱:1981462002@qq.com
    微信:zdl1994328

    4.欢迎关注我的微信公众号,最新精彩文章,及时送达:
    9414344-c474349cd3bd4b82.jpg
    公众号.jpg
  • 相关阅读:
    前端水印
    canvas中图片大小自适应
    了解CSS变量var
    vue报错Adjacent JSX elements must be wrapped in an enclosing tag.
    修改Batch3DModelContent的片元着色器:让白模穿品如的衣服
    Cesium 顶点着色器中求解模型坐标
    Centos6.5部署及yum源注意事项
    linux常用命令
    复旦大学数学学院 18 级本科生对高等代数学习方法指导书的评价
    复旦大学数学学院 17 级本科生对高等代数学习方法指导书的评价
  • 原文地址:https://www.cnblogs.com/toly-top/p/9781926.html
Copyright © 2020-2023  润新知