SoFunction
Updated on 2025-04-05

Exploration of solutions to Android BottomNavigationView and Fragment reconstruction and overlapping problems

Introduction

Under BottomNavigationView+multiple Fragment frameworks, when performing Fragment switches, Fragment reconstruction will also occur, and Fragment at the same level will not hide, resulting in overlapping

Solution

first step

Initialize a Fragment page that needs to be displayed by default

public void InitFragment(Bundle savedInstanceState) {
        //Judge whether the activity is rebuilt. If not, there is no need to rebuild the fragment.        if (savedInstanceState == null) {
            fragmentManager = getSupportFragmentManager();
            fragmentTransaction = ();
            if (mMovie == null) {
                mMovie = new HomeFragment();
            }
            CurrentFragment = mMovie;
            (.nav_host_fragment_activity_main, mMovie).commit();//fragment parent layout id
        }
    }

Step 2

Listen to BottomNavigationView switch event

 (listener);

Listen to each fragment at the same level, and when switching, show or hide its state

private  listener = new () {
        @Override
        public boolean onNavigationItemSelected(@NonNull @NotNull MenuItem item) {
            switch (()) {
                case .navigation_home:
                    if (mMovie == null) {
                        mMovie = new HomeFragment();
                    }
                    switchContent(CurrentFragment, mMovie);
                    return true;
                case .navigation_dashboard:
                    if (mExplore == null) {
                        mExplore = new ExploreFragment();
                    }
                    switchContent(CurrentFragment, mExplore);
                    return true;
                case .navigation_notifications:
                    if (mLibrary == null) {
                        mLibrary = new LibraryFragment();
                    }
                    switchContent(CurrentFragment, mLibrary);
                    return true;
                case .navigation_member:
                    if (mMember == null) {
                        mMember = new MemberFragment();
                    }
                    switchContent(CurrentFragment, mMember);
                    return true;
            }
            return false;
        }
    };

Step 3

This is to hide the original Fragment and show the Fragment to be redirected to prevent the page from overlapping

public void switchContent(Fragment from, Fragment to) {
        if (from == null || to == null) return;
        if (CurrentFragment != to) {
            CurrentFragment = to;
            fragmentManager = getSupportFragmentManager();
            fragmentTransaction = ();
            if (!()) {  
                //fragment parent layout id
                (from).add(.nav_host_fragment_activity_main, to).commit(); 
            } else {
                (from).show(to).commit(); 
            }
        }
    }

use

    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private HomeFragment mMovie = null;
    private ExploreFragment mExplore = null;
    private LibraryFragment mLibrary = null;
    private MemberFragment mMember = null;
    private Fragment CurrentFragment = null;
  InitFragment(savedInstanceState);
 (listener);

Fragment jump of the same level

When the same level Fragment in the BottomNavigationView needs to be redirected, you can use EventBus to perform cross-process communication implementation, and then get the BottomNavigationView instance to switch. This id is the Fragment page ID that needs to be redirected.

 @Subscribe(threadMode = , sticky = true)
    public void OnEvent(ChangeFragBean bean) {
        (.navigation_dashboard);
    }

Activity jump to Fragment

Also using EventBus, when jumping from an Activity to a certain Fragment of BottomNavigationView, a delay is required to execute, because the Activity may not be destroyed. The delay time is determined according to the specific mobile phone performance, which can be roughly 300-500 milliseconds.

 @Subscribe(threadMode = , sticky = true)
    public void OnEvent(MermberBean bean) {
        Handler handler = new Handler();
        (new Runnable() {
            @Override
            public void run() {
                /**
                  *Operation to be performed
                  */
                (.navigation_member);
            }
        }, 250);//Execute the run method in Runnable after 3 seconds    }

This is the article about the exploration of the reconstruction and overlapping problem solutions of Android BottomNavigationView and Fragment. This is the end. For more related contents of Android BottomNavigationView and Fragment, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!