FragmentContainerView using findNavController

后端 未结 3 1617
日久生厌
日久生厌 2020-12-08 05:01

I\'m using Android Navigation Component with bottom navigation, lint gives a warning about replacing the tag with FragmentContainerView

3条回答
  •  北海茫月
    2020-12-08 05:23

    Replace this line:

    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    

    with

    NavController navController = getNavController();
    

    Where getNavController() looks like this:

        // workaround for https://issuetracker.google.com/issues/142847973
        @NonNull
        private NavController getNavController() {
            Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
            if (!(fragment instanceof NavHostFragment)) {
                throw new IllegalStateException("Activity " + this
                        + " does not have a NavHostFragment");
            }
            return ((NavHostFragment) fragment).getNavController();
        }
    

提交回复
热议问题