0

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)

    }
}

The first photo is in the third fragment. Press the back button and the app goes back to the first fragment, as the second picture shows.

I hope to find a solution to that problem.

  • 1
    Did you read the very first [Principle of Navigation](https://developer.android.com/guide/navigation/navigation-principles#fixed_start_destination), which states that users should always back out of your app through the start destination of your app to prevent accidentally being pushed out to the launcher? – ianhanniballake Dec 27 '22 at 17:26
  • Sorry, I don't know. I just build the app with my own views – Martian Dec 28 '22 at 04:31
  • But with NavigationView, isn't it more resonable to exit the app when you press the back button? – Martian Dec 28 '22 at 04:36
  • That's exactly not what the Principles of Navigation, which are based on real user experience studies, suggest. – ianhanniballake Dec 28 '22 at 05:05
  • Okay, I see. Thanks for telling me the principles. – Martian Dec 28 '22 at 06:36

1 Answers1

0

Did you try to override onBackPressed method? Like here:

override fun onBackPressed() {
     finish()
    
}
Felipe Palma
  • 378
  • 1
  • 9