Fragment的show()、hide()和attach()、detach()切换方式生命周期过程

1.使用show()、hide()方式进行fragment切换
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.main_home:
                switchFragmentWithShowHide(0);
                break;
            case R.id.main_shop:
                switchFragmentWithShowHide(1);
                break;
            case R.id.main_community:
                switchFragmentWithShowHide(2);
                break;
            case R.id.main_mine:
                switchFragmentWithShowHide(3);
                break;
        }
    }

    private void hideAllFragment(){
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (homeFragment != null){
            transaction.hide(homeFragment);
        }
        if (shopFragment != null){
            transaction.hide(shopFragment);
        }
        if (communityFragment != null){
            transaction.hide(communityFragment);
        }
        if (mineFragment != null){
            transaction.hide(mineFragment);
        }
        transaction.commit();
    }

    private void switchFragmentWithShowHide(int position){
        hideAllFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        switch (position){
            case 0:
                if (homeFragment == null){
                    homeFragment = new HomeFragment();
                    transaction.add(R.id.main_container , homeFragment , homeFragment.getClass().getName());
                }else {
                    transaction.show(homeFragment);
                }
                break;
            case 1:
                if (shopFragment == null){
                    shopFragment = new ShopFragment();
                    transaction.add(R.id.main_container , shopFragment , shopFragment.getClass().getName());
                }else {
                    transaction.show(shopFragment);
                }
                break;
            case 2:
                if (communityFragment == null){
                    communityFragment = new CommunityFragment();
                    transaction.add(R.id.main_container , communityFragment , communityFragment.getClass().getName());
                }else {
                    transaction.show(communityFragment);
                }
                break;
            case 3:
                if (mineFragment == null){
                    mineFragment = new MineFragment();
                    transaction.add(R.id.main_container , mineFragment , mineFragment.getClass().getName());
                }else {
                    transaction.show(mineFragment);
                }
                break;
        }
        transaction.commit();
    }

点击展示HomeFragment时,由于HomeFragment是未创建状态,所以会执行HomeFragment的创建生命周期

09-26 15:56:02.090 32733-32733/com.example.testfragment E/HomeFragment: onAttach: 
09-26 15:56:02.090 32733-32733/com.example.testfragment E/HomeFragment: onCreate: 
09-26 15:56:02.091 32733-32733/com.example.testfragment E/HomeFragment: onCreateView: 
09-26 15:56:02.101 32733-32733/com.example.testfragment E/HomeFragment: onActivityCreated: 
09-26 15:56:02.101 32733-32733/com.example.testfragment E/HomeFragment: onStart: 
09-26 15:56:02.102 32733-32733/com.example.testfragment E/HomeFragment: onResume: 

继续点击展示ShopFragment时

09-26 15:56:02.090 32733-32733/com.example.testfragment E/HomeFragment: onAttach: 
09-26 15:56:02.090 32733-32733/com.example.testfragment E/HomeFragment: onCreate: 
09-26 15:56:02.091 32733-32733/com.example.testfragment E/HomeFragment: onCreateView: 
09-26 15:56:02.101 32733-32733/com.example.testfragment E/HomeFragment: onActivityCreated: 
09-26 15:56:02.101 32733-32733/com.example.testfragment E/HomeFragment: onStart: 
09-26 15:56:02.102 32733-32733/com.example.testfragment E/HomeFragment: onResume: 
09-26 15:57:33.627 32733-32733/com.example.testfragment E/HomeFragment: onHiddenChanged: true
09-26 15:57:33.629 32733-32733/com.example.testfragment E/ShopFragment: onAttach: 
09-26 15:57:33.630 32733-32733/com.example.testfragment E/ShopFragment: onCreate: 
09-26 15:57:33.630 32733-32733/com.example.testfragment E/ShopFragment: onCreateView: 
09-26 15:57:33.639 32733-32733/com.example.testfragment E/ShopFragment: onActivityCreated: 
09-26 15:57:33.639 32733-32733/com.example.testfragment E/ShopFragment: onStart: 
09-26 15:57:33.639 32733-32733/com.example.testfragment E/ShopFragment: onResume: 

当继续点击展示ShopFragment时,HomeFragment首先回调了onHiddenChanged函数,hidden参数为false
继续点击展示CommunityFragment时

09-26 15:56:02.090 32733-32733/com.example.testfragment E/HomeFragment: onAttach: 
09-26 15:56:02.090 32733-32733/com.example.testfragment E/HomeFragment: onCreate: 
09-26 15:56:02.091 32733-32733/com.example.testfragment E/HomeFragment: onCreateView: 
09-26 15:56:02.101 32733-32733/com.example.testfragment E/HomeFragment: onActivityCreated: 
09-26 15:56:02.101 32733-32733/com.example.testfragment E/HomeFragment: onStart: 
09-26 15:56:02.102 32733-32733/com.example.testfragment E/HomeFragment: onResume: 
09-26 15:57:33.627 32733-32733/com.example.testfragment E/HomeFragment: onHiddenChanged: true
09-26 15:57:33.629 32733-32733/com.example.testfragment E/ShopFragment: onAttach: 
09-26 15:57:33.630 32733-32733/com.example.testfragment E/ShopFragment: onCreate: 
09-26 15:57:33.630 32733-32733/com.example.testfragment E/ShopFragment: onCreateView: 
09-26 15:57:33.639 32733-32733/com.example.testfragment E/ShopFragment: onActivityCreated: 
09-26 15:57:33.639 32733-32733/com.example.testfragment E/ShopFragment: onStart: 
09-26 15:57:33.639 32733-32733/com.example.testfragment E/ShopFragment: onResume: 
09-26 16:01:10.822 32733-32733/com.example.testfragment E/ShopFragment: onHiddenChanged: true
09-26 16:01:10.823 32733-32733/com.example.testfragment E/CommunityFragment: onAttach: 
09-26 16:01:10.823 32733-32733/com.example.testfragment E/CommunityFragment: onCreate: 
09-26 16:01:10.824 32733-32733/com.example.testfragment E/CommunityFragment: onCreateView: 
09-26 16:01:10.828 32733-32733/com.example.testfragment E/CommunityFragment: onActivityCreated: 
09-26 16:01:10.828 32733-32733/com.example.testfragment E/CommunityFragment: onStart: 
09-26 16:01:10.828 32733-32733/com.example.testfragment E/CommunityFragment: onResume: 

同理ShopFragment回调onHiddenChanged函数,hidden参数为false,然后再执行CommunityFragment创建的生命周期,一次类推MineFragment也是一样

当再次点击回HomeFragment时,由于HomeFragment已经创建,不会再去执行fragment的生命周期函数,只会执行onHiddenChanged函数

09-26 16:04:49.575 32733-32733/com.example.testfragment E/CommunityFragment: onHiddenChanged: true
09-26 16:04:49.581 32733-32733/com.example.testfragment E/HomeFragment: onHiddenChanged: false

所以使用show()、hide()函数进行fragment切换时,只会在fragment创建的时候执行生命周期函数,再次切换的时候只会调用onHiddenChanged函数,onResume()、onStop()函数根本不会调用,fragment的实例状态就在那里,只是一个显示,其他全部隐藏,所以当在fragment每次展示都需要刷新数据的时候不要在onResume()函数中进行数据刷新操作,而是在onHiddenChanged函数中进行

2.使用attach()、detach()函数进行fragment切换
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.main_home:
                switchFragmentWithAttachDetach(0);
                break;
            case R.id.main_shop:
                switchFragmentWithAttachDetach(1);
                break;
            case R.id.main_community:
                switchFragmentWithAttachDetach(2);
                break;
            case R.id.main_mine:
                switchFragmentWithAttachDetach(3);
                break;
        }
    }

    private void detachAllFragment(){
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (homeFragment != null){
            transaction.detach(homeFragment);
        }
        if (shopFragment != null){
            transaction.detach(shopFragment);
        }
        if (communityFragment != null){
            transaction.detach(communityFragment);
        }
        if (mineFragment != null){
            transaction.detach(mineFragment);
        }
        transaction.commit();
    }

    private void switchFragmentWithAttachDetach(int position){
        detachAllFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        switch (position){
            case 0:
                if (homeFragment == null){
                    homeFragment = new HomeFragment();
                    transaction.add(R.id.main_container , homeFragment , homeFragment.getClass().getName());
                }else {
                    transaction.attach(homeFragment);
                }
                break;
            case 1:
                if (shopFragment == null){
                    shopFragment = new ShopFragment();
                    transaction.add(R.id.main_container , shopFragment , shopFragment.getClass().getName());
                }else {
                    transaction.attach(shopFragment);
                }
                break;
            case 2:
                if (communityFragment == null){
                    communityFragment = new CommunityFragment();
                    transaction.add(R.id.main_container , communityFragment , communityFragment.getClass().getName());
                }else {
                    transaction.attach(communityFragment);
                }
                break;
            case 3:
                if (mineFragment == null){
                    mineFragment = new MineFragment();
                    transaction.add(R.id.main_container , mineFragment , mineFragment.getClass().getName());
                }else {
                    transaction.attach(mineFragment);
                }
                break;
        }
        transaction.commit();
    }

点击展示HomeFragment时,

09-26 16:11:08.405 10146-10146/com.example.testfragment E/HomeFragment: onAttach: 
09-26 16:11:08.405 10146-10146/com.example.testfragment E/HomeFragment: onCreate: 
09-26 16:11:08.406 10146-10146/com.example.testfragment E/HomeFragment: onCreateView: 
09-26 16:11:08.414 10146-10146/com.example.testfragment E/HomeFragment: onActivityCreated: 
09-26 16:11:08.414 10146-10146/com.example.testfragment E/HomeFragment: onStart: 
09-26 16:11:08.414 10146-10146/com.example.testfragment E/HomeFragment: onResume: 

与show()、hide()方式相同,执行创建的生命周期

由HomeFragment切换至ShopFragment时,

09-26 16:11:08.405 10146-10146/com.example.testfragment E/HomeFragment: onAttach: 
09-26 16:11:08.405 10146-10146/com.example.testfragment E/HomeFragment: onCreate: 
09-26 16:11:08.406 10146-10146/com.example.testfragment E/HomeFragment: onCreateView: 
09-26 16:11:08.414 10146-10146/com.example.testfragment E/HomeFragment: onActivityCreated: 
09-26 16:11:08.414 10146-10146/com.example.testfragment E/HomeFragment: onStart: 
09-26 16:11:08.414 10146-10146/com.example.testfragment E/HomeFragment: onResume: 
09-26 16:13:26.856 10146-10146/com.example.testfragment E/HomeFragment: onPause: 
09-26 16:13:26.857 10146-10146/com.example.testfragment E/HomeFragment: onStop: 
09-26 16:13:26.857 10146-10146/com.example.testfragment E/HomeFragment: onDestroyView: 
09-26 16:13:26.864 10146-10146/com.example.testfragment E/ShopFragment: onAttach: 
09-26 16:13:26.864 10146-10146/com.example.testfragment E/ShopFragment: onCreate: 
09-26 16:13:26.865 10146-10146/com.example.testfragment E/ShopFragment: onCreateView: 
09-26 16:13:26.869 10146-10146/com.example.testfragment E/ShopFragment: onActivityCreated: 
09-26 16:13:26.869 10146-10146/com.example.testfragment E/ShopFragment: onStart: 
09-26 16:13:26.869 10146-10146/com.example.testfragment E/ShopFragment: onResume: 

在创建ShopFragment实例之前,HomeFragment执行了onPause()、onStop()、onDestoryView()三个生命周期函数,由此可知HomeFragment将其视图进行了销毁,但是其与Activity的关联不会断开,不会执行onDetach()函数

继续点击展示CommunityFragment时,

09-26 16:11:08.405 10146-10146/com.example.testfragment E/HomeFragment: onAttach: 
09-26 16:11:08.405 10146-10146/com.example.testfragment E/HomeFragment: onCreate: 
09-26 16:11:08.406 10146-10146/com.example.testfragment E/HomeFragment: onCreateView: 
09-26 16:11:08.414 10146-10146/com.example.testfragment E/HomeFragment: onActivityCreated: 
09-26 16:11:08.414 10146-10146/com.example.testfragment E/HomeFragment: onStart: 
09-26 16:11:08.414 10146-10146/com.example.testfragment E/HomeFragment: onResume: 
09-26 16:13:26.856 10146-10146/com.example.testfragment E/HomeFragment: onPause: 
09-26 16:13:26.857 10146-10146/com.example.testfragment E/HomeFragment: onStop: 
09-26 16:13:26.857 10146-10146/com.example.testfragment E/HomeFragment: onDestroyView: 
09-26 16:13:26.864 10146-10146/com.example.testfragment E/ShopFragment: onAttach: 
09-26 16:13:26.864 10146-10146/com.example.testfragment E/ShopFragment: onCreate: 
09-26 16:13:26.865 10146-10146/com.example.testfragment E/ShopFragment: onCreateView: 
09-26 16:13:26.869 10146-10146/com.example.testfragment E/ShopFragment: onActivityCreated: 
09-26 16:13:26.869 10146-10146/com.example.testfragment E/ShopFragment: onStart: 
09-26 16:13:26.869 10146-10146/com.example.testfragment E/ShopFragment: onResume: 
09-26 16:18:50.568 10146-10146/com.example.testfragment E/ShopFragment: onPause: 
09-26 16:18:50.569 10146-10146/com.example.testfragment E/ShopFragment: onStop: 
09-26 16:18:50.569 10146-10146/com.example.testfragment E/ShopFragment: onDestroyView: 
09-26 16:18:50.572 10146-10146/com.example.testfragment E/CommunityFragment: onAttach: 
09-26 16:18:50.572 10146-10146/com.example.testfragment E/CommunityFragment: onCreate: 
09-26 16:18:50.574 10146-10146/com.example.testfragment E/CommunityFragment: onCreateView: 
09-26 16:18:50.578 10146-10146/com.example.testfragment E/CommunityFragment: onActivityCreated: 
09-26 16:18:50.578 10146-10146/com.example.testfragment E/CommunityFragment: onStart: 
09-26 16:18:50.579 10146-10146/com.example.testfragment E/CommunityFragment: onResume: 

同理会先执行ShopFragment的视图销毁生命周期,再执行CommunityFragment的创建生命周期;MineFragment同理

当再次点击回HomeFragment时,

09-26 16:21:18.359 10146-10146/com.example.testfragment E/CommunityFragment: onPause: 
09-26 16:21:18.359 10146-10146/com.example.testfragment E/CommunityFragment: onStop: 
09-26 16:21:18.359 10146-10146/com.example.testfragment E/CommunityFragment: onDestroyView: 
09-26 16:21:18.361 10146-10146/com.example.testfragment E/HomeFragment: onCreateView: 
09-26 16:21:18.362 10146-10146/com.example.testfragment E/HomeFragment: onActivityCreated: 
09-26 16:21:18.362 10146-10146/com.example.testfragment E/HomeFragment: onStart: 
09-26 16:21:18.362 10146-10146/com.example.testfragment E/HomeFragment: onResume: 

会执行上一个fragment的onPause()、onStop()、onDestoryView()函数进行视图销毁操作,当展示下一个fragment时由于实例已经存在会执行onCreateView()、onActivityCreated()、onStart()、onResume()函数

可知使用attach()、detach()函数进行fragment切换时,会将fragment的视图销毁,再次展示时会进行fragment的视图重绘操作,但是其与fragment的关联不会断开

你可能感兴趣的:(android开发)