So I am using Navigation Component with Menu items to create a BottomNavigation bar . However the problem occurs when i have fully run the error free code and the menu items are completely unresponsive. It only remains stagnant in one Fragment without navigating to others when clicking.
Here is the activity where the navhost fragment is located:
class HomeActivity : AppCompatActivity() {
private lateinit var navController: NavController
private val binding: ActivityHomeBinding by lazy {
ActivityHomeBinding.inflate(layoutInflater)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
setUpBottomNavigation()
}
private fun setUpBottomNavigation() {
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.fragmentContainerView) as NavHostFragment
navController = navHostFragment.findNavController()
binding.bottomNavigation.setupWithNavController(navController)
}
}
Here is the xml:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Home.HomeActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="409dp"
android:layout_height="673dp"
app:defaultNavHost="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/nav_graph_without_splash" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="411dp"
android:layout_height="wrap_content"
android:visibility="visible"
app:backgroundTint="@color/lightGray"
app:elevation="8dp"
app:itemIconTint="@color/bottom_nav_tint"
app:itemTextColor="@color/bottom_nav_tint"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
heres the menu item.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/start2"
android:title="Home"
android:icon="@drawable/ic_home"/>
<item
android:id="@+id/data"
android:title="Data"
android:icon="@drawable/ic_baseline_mms_24"/>
<item
android:id="@+id/profile"
android:title="Profile"
android:icon="@drawable/ic_profile"/>
Start by doing this in your activity file
class LoginFragment : Fragment() {
private var _binding: FragmentLoginBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentLoginBinding.inflate(inflater, container, false)
binding.tohomepagebutton.setOnClickListener {
/*
since you are using nav graph instead of using intents to switch between activities,
you can invoke the actions on the navgraph to navigate to the preferred destination
to understand better check https://medium.com/swlh/android-navigation-component-part-1-6191323eaf39
https://developer.android.com/guide/navigation/navigation-getting-started*/
NavHostFragment.findNavController(this).navigate(R.id.action_loginFragment_to_home2)
}
return binding.root
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}