当我导航到新目的地时如何禁用向上按钮

How to disable Up button when I navigate to a new 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>

但是当我导航到 secondFragment 时,TopLeft 中的后退按钮始终在这里并且没有消失。

更新

我的主要 activity 显示如下。

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)
    }
}

顺便说一句,我想禁用 firstFragment 中的抽屉切换按钮,它似乎也不起作用。

在你的activity中:

// set action bar in your activity
setSupportActionBar(toolbar)

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

    // check if navigated to second fragment, then disable the back button
    if(destination.id == R.id.secondFragment) {
        supportActionBar?.setDisplayHomeAsUpEnabled(false)
        supportActionBar?.setHomeButtonEnabled(false)
    }
 }

你可以查看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

如果您想自定义哪些目的地被视为顶级目的地,您可以将一组目的地 ID 传递给构造函数,例如:

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

关于 DrawerLayout.
如果目的地不使用 DrawerLayout,导航按钮将被隐藏。