简体   繁体   English

按钮返回带有导航组件的 startDestination

[英]button back in startDestination with navigation component

I need a second activity with a nav graph and have a return button in toolbar to the first activity that also contains a nav graph我需要一个带有导航图的第二个活动,并在工具栏中有一个返回按钮到第一个也包含导航图的活动

In my second activity I have onSupportNavigateUp and setupActionBarWithNavController when entering the fragments if the arrow back button appears but in the activity no.在我的第二个活动中,如果出现后退箭头按钮但在活动中没有,我在进入片段时有 onSupportNavigateUp 和 setupActionBarWithNavController。

Try adding setHomeButtonEnabled and setDisplayHomeAsUpEnabled in both the activity and the fragment and if the button appears back, but when I enter some fragment in front and return to the fragment startDestination disappears the button back尝试在活动和片段中添加 setHomeButtonEnabled 和 setDisplayHomeAsUpEnabled ,如果按钮出现回来,但是当我在前面输入一些片段并返回片段时, startDestination 按钮消失了

I just need to keep the button back in the activity and solve my problem我只需要将按钮保持在活动中并解决我的问题

You can do it by specifying a setFallbackOnNavigateUpListener:您可以通过指定 setFallbackOnNavigateUpListener 来实现:

private fun setupToolbar() {
    val navController = findNavController(R.id.nav_host_fragment)

    val appBarConfiguration =
        AppBarConfiguration.Builder()
            .setFallbackOnNavigateUpListener { onNavigateUp() }
            .build()

    dataBinding.toolbar.apply {
        setupWithNavController(navController, appBarConfiguration)
    }
}

And then do whatever you want in the Activity's:然后在活动中做任何你想做的事情:

override fun onNavigateUp(): Boolean {
    finish()
    return true
}

You can't, activity has their own toolbars and in your case they have two different NavControllers.你不能,活动有自己的工具栏,在你的情况下,他们有两个不同的 NavControllers。 So your second activity manage NavUp Button for his fragment and when start Destination fragment comes NavUpButton(Backbutton) disappear because it has no destination left behind.因此,您的第二个活动为他的片段管理 NavUp 按钮,当开始目标片段出现时,NavUpButton(Backbutton) 消失,因为它没有留下任何目的地。 And if you programmatically show NavUp Button on start destination of that (2nd activity) and manage onClick and start first activity that always goes to Start destination of first activity's fragment because it has it's own Nav Controller.如果您以编程方式在该(第二个活动)的开始目标上显示 NavUp 按钮并管理 onClick 并启动第一个活动,该活动总是转到第一个活动片段的开始目标,因为它有自己的导航控制器。

Problem is that Navigation UI not works like that.问题是导航 UI 不是那样工作的。 The better approach is use only one activity with multiple fragments.更好的方法是仅使用一个具有多个片段的活动。 And use any other approach to solve your problem within the same nav controller.并使用任何其他方法在同一个导航控制器中解决您的问题。

Add setHomeButtonEnabled func.添加setHomeButtonEnabled功能。 to your returning action.到您的返回操作。 If you are returning with button add it to onClick or with backPress, override backPress.如果您使用按钮返回,将其添加到 onClick 或使用 backPress,覆盖 backPress。

With this solve : You will set your button enable, when you try to return your startDestination.使用此解决方案:当您尝试返回 startDestination 时,您将设置按钮启用。

I created an interface to show/hide up button from the nav host activity.我创建了一个界面来显示/隐藏导航主机活动中的按钮。 Here is how the activity implements the interface methods to show/hide up button:以下是活动如何实现显示/隐藏按钮的界面方法:

    override fun showUpButton() {
    val navController = this.findNavController(R.id.nav_host)
    val listener = AppBarConfiguration.OnNavigateUpListener { navController.navigateUp() }
    val abc = AppBarConfiguration.Builder().setFallbackOnNavigateUpListener(listener).build()
    NavigationUI.setupActionBarWithNavController(this, navController, abc)
}


override fun hideUpButton() {
    val navController = this.findNavController(R.id.nav_host)
    NavigationUI.setupActionBarWithNavController(this, navController)
}

Here the method in the activity when up button pressed:这是按下向上按钮时活动中的方法:

    override fun onSupportNavigateUp(): Boolean {
    val navController = this.findNavController(R.id.nav_host)
    if(!navController.navigateUp()){ // When in start destination
        onBackPressed()
    }
    return navController.navigateUp()
}

In a fragment can listen whenever back button (NOT up button) pressed:每当按下后退按钮(不是向上按钮)时,片段都可以收听:

    private fun setupBackPress() {
    requireActivity()
        .onBackPressedDispatcher
        .addCallback(this, object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() { 
            }
        })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 导航组件 - BottomNavigationView - 返回时不要保留 startDestination 片段 - Navigation Component - BottomNavigationView - Don't keep startDestination fragment when back 带有底部导航的 Android 导航组件不会破坏 startDestination 片段 - Android Navigation Component with Bottom Navigation doesn't destroy startDestination Fragment 导航架构组件 - 使用 Fragment 将参数数据传递给 startDestination - Navigation Architecture Component- Passing argument data to the startDestination with Fragment Android 导航架构组件:如何将捆绑数据传递给 startDestination - Android Navigation Architecture Component: How to pass bundle data to startDestination 导航架构组件 - 将参数数据传递给 startDestination - Navigation Architecture Component- Passing argument data to the startDestination 使用 compose 确定导航中是否位于 startDestination - determine if is at startDestination in navigation with compose 导航: startDestination 中的捆绑包是 null - Navigation: bundle in startDestination is null 使用导航组件通过后退按钮向上导航 - Navigate up by back button with Navigation Component 处理 Android 导航组件中的后退按钮 - Handling back button in Android Navigation Component 为什么不使用android导航组件工作后退按钮 - Why not work back button with android navigation component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM