Пытаюсь сделать Navigation Component + Bottom Navigation View. Не понимаю, почему в main activity при запуске приложения не отображается NavHostFragment. В визуальном редакторе все как положено.
Ниже привожу разметку main activity:
<?xml version="1.0" encoding="utf-8"?>
<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=".presentation.view.MainActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:backgroundTint="#FFFFFF"
app:backgroundTint="@color/purple_500"
app:itemIconTint="@drawable/bottom_nav_selector"
app:itemTextColor="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_menu" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:name="androidx.navigation.fragment.NavHostFragment"
app:defaultNavHost="true"
app:navGraph="@navigation/navigator"/>
Код самой активити ( пробовала убрать все из onCreate, не помогло ):
class MainActivity : AppCompatActivity () {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController
val appBarConfiguration = AppBarConfiguration(setOf(R.id.petFragment2,R.id.tasksFragment2,R.id.settingsFragment2))
val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_nav)
setupActionBarWithNavController(navController, appBarConfiguration)
bottomNavigationView.setupWithNavController(navController)
}
}
А вот и сам navigation:
<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/navigator"
app:startDestination="@id/petFragment2">
<fragment
android:id="@+id/settingsFragment2"
android:name="com.android.petdiary.presentation.view.SettingsFragment"
android:label="fragment_settings"
tools:layout="@layout/fragment_settings" />
<fragment
android:id="@+id/tasksFragment2"
android:name="com.android.petdiary.presentation.view.TasksFragment"
android:label="fragment_tasks"
tools:layout="@layout/fragment_tasks" />
<fragment
android:id="@+id/petFragment2"
android:name="com.android.petdiary.presentation.view.PetFragment"
android:label="fragment_pet"
tools:layout="@layout/fragment_pet" />
</navigation>