The difference between replace() and add() methods when Android Fragment is dynamically created

1. replace() as the name implies "replace" will destroy the existing views in the layout container, which will cause reinitialization and waste traffic every time the Fragment is switched.

//Get fragment Manager
fragmentManager = getSupportFragmentManager();

//Transactions cannot be shared. Each time a transaction is used, it must be reopened and then submitted
FragmentTransaction fragmentTransactiontwo = fragmentManager.beginTransaction();
//Parameters: 1. Parent container 2. fragment to replace.
fragmentTransactiontwo.replace(R.id.framelayout, fragmentTwo);
//Submission of affairs
fragmentTransactiontwo.commit();

Therefore, if you have a high probability of using the current Fragment again, it is recommended to use add() with show(),hide(), which can improve the user experience.

2. add() is to add fragments to the container layout, and then display and hide them with the show() and hide() methods of the transaction object. They finally make the FragmentsetVisibility (true or false) without calling the lifecycle.

     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();

Hide the current fragment before jump, and display the frment to be displayed:

/*3,Initialize bottom navigation menu*/
    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;
            }
        });
    }



Keywords: Fragment

Added by sevenmenkes on Sat, 18 Apr 2020 19:06:07 +0300