I basically set up 3 fragments for my bottom navigation view with all linked to activity.xml
activity.xml where I put fragment tag.
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView2"
app:navGraph="@navigation/my_nav"
android:layout_width="409dp"
android:layout_height="599dp" />
My Activity.java code (specifically):
BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);
NavController navController = Navigation.findNavController(this, R.id.fragment);
NavigationUI.setupWithNavController(bottomNavigationView, navController);
The problem now is that if I run that same java code using fragment tag in xml, it runs well but suggests I use <androidx.fragment.app.FragmentContainerView(linters)
but on using <androidx.fragment.app.FragmentContainerView
, it displays the error in the Logcat.
Activity does not have a NavController set on
I've seen a lot of similar errors and fixes for that on this site like FragmentContainerView as NavHostFragment
But the problem now is that most of them post Kotlin codes and the few java codes I tried didn't work for me
or who can translate this code to java:
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController
NavController manages app navigation within a NavHost . Apps will generally obtain a controller directly from a host, or by using one of the utility methods on the Navigation class rather than create a controller directly. Navigation flows and destinations are determined by the navigation graph owned by the controller.
onSupportNavigateUp() This method is called whenever the user chooses to navigate Up within your application's activity hierarchy from the action bar. @Nullable ActionMode.
I solved this by first inflating the view which contains the NavHost, then using the supportFragmentManager to get the navHost Fragment and pass in my default args. Hoping that Google's Android team provides a cleaner solution to this in the future. Show activity on this post. Show activity on this post.
The Openable layout indicating that the Navigation button should be displayed as a drawer symbol when it is not being shown as an Up button. Returns. Openable. The Openable layout that should be toggled from the Navigation button.
You can get the Nav Controller using Java like this:
final NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
final NavController navController = navHostFragment.getNavController();
And then you setup your bottom navigation like this:
NavigationUI.setupWithNavController(bottomNavigationView, navController);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With