BottomNavigationView with NavigationUI and multiple fragments will re-creation

Viewed 54

When i change BottomNavigation tab , the fragment will be re-creation.

How to cache the created fragment and not re-creation?

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        bottomNavigationView = view.findViewById(R.id.bottom_navigation);
        NavController navController = Navigation.findNavController(getActivity(), R.id.bottom_nav_host_fragment);
        NavigationUI.setupWithNavController(bottomNavigationView, navController);
        this.initBottomNavigation();
    }

 private void initBottomNavigation() {
        bottomNavigationView.setOnNavigationItemSelectedListener(item -> {
        
            NavController navController = Navigation.findNavController(getActivity(), R.id.bottom_nav_host_fragment);
            return NavigationUI.onNavDestinationSelected(item, navController)
                    || super.onOptionsItemSelected(item);

}
0 Answers
Related