使用NavigationUI 加载Fragment,如何获取Fragment对象

1.  NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
//        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        navView.setItemIconTintList(null);   // 设置 BottomNavigationView 图片没有颜色覆盖
        NavigationUI.setupWithNavController(navView, navController);

2.  获取Fragment的代码

public Fragment getFragment(Class clazz) {
    List fragments = getSupportFragmentManager().getFragments();
    if (fragments!= null && fragments.size() > 0) {
       NavHostFragment navHostFragment = (NavHostFragment) fragments.get(0);
       List childfragments = navHostFragment.getChildFragmentManager().getFragments();
       if(childfragments != null && childfragments.size() > 0){
           for (int j = 0; j < childfragments.size(); j++) {
               Fragment fragment = childfragments.get(j);
               if(fragment.getClass().isAssignableFrom(clazz)){
                   Log.i(TAG, "getFragment1: " + fragment);
                   return fragment;
               }
           }
       }
    }
    return null;
}

你可能感兴趣的:(work遇到问题)