Fragment re-created on bottom navigation view item selected

后端 未结 15 2566
长发绾君心
长发绾君心 2020-12-07 23:55

Following is my code for bottom navigation view item selected

bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationI         


        
15条回答
  •  庸人自扰
    2020-12-08 00:31

    There are several test cases involved in proper navigation , I am pasting my code with all test cases checked.

    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            switch (item.getItemId()) {
                case R.id.dashboard:
                    Fragment fragment;
                    fragment = fragmentManager.findFragmentByTag(DashboardFragment.TAG);
    
                    if (fragment == null) {
                        fragment = new DashboardFragment();
                        fragmentTransaction.add(R.id.frame, fragment, DashboardFragment.TAG);
                    } else {
                        fragmentTransaction.detach(fragmentManager.getPrimaryNavigationFragment());
                        fragmentTransaction.attach(fragment);
                    }
                    fragmentTransaction.setPrimaryNavigationFragment(fragment);
                    fragmentTransaction.commitNow();
                    return true;
                case R.id.expenses:
                    fragment = fragmentManager.findFragmentByTag(ExpenseFragment.TAG);
                    if (fragment == null) {
                        fragment = new ExpenseFragment();
                        fragmentTransaction.add(R.id.frame, fragment, ExpenseFragment.TAG);
                    } else {
                        fragmentTransaction.detach(fragmentManager.getPrimaryNavigationFragment());
                        fragmentTransaction.attach(fragment);
                    }
                    fragmentTransaction.setPrimaryNavigationFragment(fragment);
                    fragmentTransaction.commitNow();
                    return true;
                case R.id.vehicle_parts:
                    Bundle bundle = new Bundle();
                    bundle.putInt("odometer", vehicle.getOdometer());
                    bundle.putString("vehicle_id", vehicle.get_id());
                    fragment = fragmentManager.findFragmentByTag(PartsFragment.TAG);
                    if (fragment == null) {
                        fragment = new PartsFragment();
                        fragment.setArguments(bundle);
                        fragmentTransaction.add(R.id.frame, fragment, PartsFragment.TAG);
    
                    } else {
                        fragmentTransaction.detach(fragmentManager.getPrimaryNavigationFragment());
                        fragmentTransaction.attach(fragment);
                    }
                    fragmentTransaction.setPrimaryNavigationFragment(fragment);
                    fragmentTransaction.commitNow();
                    return true;
                case R.id.blog:
                    fragment = fragmentManager.findFragmentByTag(BlogFragment.TAG);
    
                    if (fragment == null) {
                        fragment = new BlogFragment();
                        fragmentTransaction.add(R.id.frame, fragment, BlogFragment.TAG);
    
                    } else {
                        fragmentTransaction.detach(fragmentManager.getPrimaryNavigationFragment());
                        fragmentTransaction.attach(fragment);
                    }
                    fragmentTransaction.setPrimaryNavigationFragment(fragment);
                    fragmentTransaction.commitNow();
                    return true;
            }
            return false;
    

提交回复
热议问题