I use BottomNavigationView with navgation graph and I expect that when I press the back button, I'll exit the app. But that only works when I'm in the first fragment.
When I'm in other fragments, I press the back button and I'll go back to the first fragment. To exit the app, I need to press the back button again.
How to fix that?
That's my MainActivity's Layout,just a BottomNavigationView and a FragmentContainerView:
<?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:id="@+id/main_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.MainActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
app:labelVisibilityMode="auto"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/menu_main_bottom" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/nav_main_bottom" />
</androidx.constraintlayout.widget.ConstraintLayout>
My navigation graph, nav_main_bottom:
<?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/main_nav_bottom"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="com.yuan.order.home.HomeFragment"
android:label="fragment_home"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/manageFragment"
android:name="com.yuan.order.manage.ManageFragment"
android:label="fragment_message"
tools:layout="@layout/fragment_manage" />
<fragment
android:id="@+id/infoFragment"
android:name="com.yuan.order.info.InfoFragment"
android:label="fragment_info"
tools:layout="@layout/fragment_info" />
</navigation>
That's my MainActivity, I used ViewBinding:
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.host_fragment) as NavHostFragment
binding.bottomNavigationView.setupWithNavController(navHostFragment.navController)
}
}
I hope to find a solution to that problem.