Custom “navigate up” behavior for certain fragment using Navigation component

前端 未结 4 410
心在旅途
心在旅途 2021-02-04 01:58

I want to add custom up navigation from fragment using Navigation component

In my build.gradle(app) I use androidx.appcompat:appcompat:1.1.0-

4条回答
  •  佛祖请我去吃肉
    2021-02-04 02:27

    This set up also works and you won't need to override onSupportNavigateUp in your activity:

    NavigationUI.setupWithNavController(toolbar, navController, appBarConfiguration)
    toolbar.setNavigationOnClickListener {
        if (navController.currentDestination?.id == R.id.destinationOfInterest) {
            // Custom behavior here
        } else {
            NavigationUI.navigateUp(navController, configuration)
        }
    }
    

    I prefer to set up the Toolbar since it will handle automatically the up navigation and open/close a DrawerLayout if you have one.

提交回复
热议问题