Search code examples
androidandroid-architecture-navigationandroid-jetpack-navigation

How to disable Up button when I navigate to a new destination


I tried to disable the up button when navigate to a new destination, and make this destination as a new start destination.

// navigation.xml
<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/navigation"
    app:startDestination="@id/firstFragment">

    <fragment
        android:id="@+id/firstFragment"
        android:name="com.example.myproject.FirstFragment"
        android:label="FirstFragment" >
        <action
            android:id="@+id/action_firstFragment_to_secondFragment"
            app:destination="@id/secondFragment"
            app:popUpTo="@id/firstFragment"
            app:popUpToInclusive="true" />
    </fragment>
    <fragment
        android:id="@+id/secondFragment"
        android:name="com.example.myoroject.SecondFragment"
        android:label="fragment_second"
        tools:layout="@layout/fragment_second" />
</navigation>

But when I navigate to secondFragment, the back button in TopLeft is always here and not disappear.

UPDATE

My main activity show below.

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    private lateinit var navController: NavController
    private lateinit var drawerLayout: DrawerLayout

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
        drawerLayout = binding.drawerLayout
        navController = this.findNavController(R.id.myNavHostFragment)

        navController.addOnDestinationChangedListener { _, destination, _ ->
            if(destination.id == R.id.firstFragment) {
                drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_CLOSED)
            } else {
                drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
            }
        }

        NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout)
        NavigationUI.setupWithNavController(binding.navView, navController)
    }

    override fun onSupportNavigateUp(): Boolean {
        return NavigationUI.navigateUp(navController, drawerLayout)
    }
}

BTW I want to disable the drawer toggle button in firstFragment, it also seems not work.


Solution

  • You can check the doc:

    Top-level destinations do not display an Up button in the top app bar because there is no higher level destination.
    When the user is on any other destination, the Navigation button appears as an Up button . To configure the Navigation button using only the start destination as the top-level destination, create an AppBarConfiguration object

    If you want to customize which destinations are considered top-level destinations, you can pass a set of destination IDs to the constructor, something like:

    val appBarConfiguration = AppBarConfiguration(setOf(R.id.firstFragment, R.id.secondFragment))
    setupActionBarWithNavController(navigationController, appBarConfiguration)
    

    About the DrawerLayout.
    If the destination doesn't use a DrawerLayout, the Navigation button is hidden.