I have created from Android studio a new Android ( Kotlin) project based on the « Bottom Navigation View Activity » template.
So, 1 activity with its XML template that contains the navigation part and 1 fragment only :
<?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"
android:id="@+id/container"
android:layout_width="match_parent"
android:background="@color/white"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
<fragment
android:id="@+id/nav_host_fragment_activity_main"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
When the first fragment ( “1” on the screenshot) is displayed, I want to add a button to navigate to another fragment, and allow the user , when clicking on the “back” button of the device, to come back to the original fragment. I tried with the fragmentManager approach as described in the official documentation without any results:
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.padding(10.dp)
.border(1.dp, Color.DarkGray, RoundedCornerShape(5.dp))
.background(Color.LightGray)
.padding(10.dp)
.fillMaxWidth()
.clickable {
fragment.parentFragmentManager.commit {
replace<ComboListFragment>(R.id.nav_host_fragment_activity_main)
setReorderingAllowed(true)
addToBackStack("name") // Name can be null
}
...