Fragment re-created on bottom navigation view item selected

后端 未结 15 2567
长发绾君心
长发绾君心 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:46

    With support library v26 you can do this

    FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
    
    Fragment curFrag = mFragmentManager.getPrimaryNavigationFragment();
    if (curFrag != null) {
        fragmentTransaction.detach(curFrag);
    }
    
    Fragment fragment = mFragmentManager.findFragmentByTag(tag);
    if (fragment == null) {
        fragment = new YourFragment();
        fragmentTransaction.add(container.getId(), fragment, tag);
    } else {
        fragmentTransaction.attach(fragment);
    }
    
    fragmentTransaction.setPrimaryNavigationFragment(fragment);
    fragmentTransaction.setReorderingAllowed(true);
    fragmentTransaction.commitNowAllowingStateLoss();
    

提交回复
热议问题