Android Fragment动态创建时replace()和add()方法的区别

1、replace()顾名思义“替换”,会销毁布局容器内的已有视图,这样会导致每次切换Fragment时都会重新初始化,浪费流量。

//获取碎片管理者
fragmentManager = getSupportFragmentManager();

//事务是不能共享的,每次用到都要重新开启一个事务,之后提交
FragmentTransaction fragmentTransactiontwo = fragmentManager.beginTransaction();
//参数:1.父容器   2.要替换的fragment。
fragmentTransactiontwo.replace(R.id.framelayout, fragmentTwo);
//提交事务
fragmentTransactiontwo.commit();

因此,如果你有一个很高的概率会再次使用当前的Fragment,建议使用add()搭配show(),hide()使用,可以提高用户体验

2、add()是添加Fragment于容器布局中,再搭配事务对象的show()和hide()方法来显示和隐藏,它们最终是让FragmentsetVisibility(true还是false),不会调用生命周期。

     homepageFragment = new HomepageFragment();
     hotFragment = new HotFragment();
     meFragment = new MeFragment();

     getSupportFragmentManager().beginTransaction()
              .add(R.id.content_fragment_layout,homepageFragment, "homepageFragment")
              .add(R.id.content_fragment_layout,hotFragment,"hotFragment")
              .add(R.id.content_fragment_layout,meFragment,"meFragment")
              .show(homepageFragment)
              .hide(hotFragment)
              .hide(meFragment)
              .commit();

     在跳转前将当前的fragment隐藏,要显示的frment进行显示:

/*3、初始化底部导航菜单*/
    private void initBottomNavigationView()
    {
        bottomNavigationView= (BottomNavigationView)findViewById(R.id.bottom_navigation);
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener()
        {
            @Override
            public boolean onNavigationItemSelected(MenuItem item)
            {
                switch (item.getItemId())
                {
                    case R.id.bottom_navigation_homepage:
                        topView.setVisibility(View.VISIBLE);
                        toolbar.setVisibility(View.VISIBLE);

                        getSupportFragmentManager().beginTransaction()
                                .show(homepageFragment)
                                .hide(hotFragment)
                                .hide(meFragment)
                                .commit();

                        break;

                    case R.id.bottom_navigation_find:
                        topView.setVisibility(View.GONE);
                        toolbar.setVisibility(View.GONE);

                        getSupportFragmentManager().beginTransaction()
                                .show(hotFragment)
                                .hide(homepageFragment)
                                .hide(meFragment)
                                .commit();
                        break;

                    case R.id.bottom_navigation_me:
                        topView.setVisibility(View.GONE);
                        toolbar.setVisibility(View.GONE);

                        getSupportFragmentManager().beginTransaction()
                                .show(meFragment)
                                .hide(homepageFragment)
                                .hide(hotFragment)
                                .commit();
                        break;
                }
                return true;
            }
        });
    }



你可能感兴趣的:(Android)