导航视图全局操作返回状态 [英] Navigation View Global Action Back State

查看:161
本文介绍了导航视图全局操作返回状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在弄弄Jetpack导航组件,并创建了一个使用导航抽屉的活动.

I have been messing with the Jetpack Navigation component, and I created an activity that uses a navigation drawer.

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val navigationController = findNavController(R.id.navigationHostFragment)
        navigationView.setupWithNavController(navigationController)

        val appBarConfiguration = AppBarConfiguration(TOP_LEVEL_DESTINATIONS, drawerLayout)
        toolbar.setupWithNavController(navigationController, appBarConfiguration)
    }

    override fun onSupportNavigateUp(): Boolean =
            NavigationUI.navigateUp(findNavController(R.id.navigationHostFragment), drawerLayout)

}

这符合我的预期,但是随后我添加了全局操作进入设置屏幕.

This works as I would expect, but then I added a global action for the settings screen.

<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"
    app:startDestination="@id/appBarFragment">

    ...

    <activity android:id="@+id/settingsActivity"
        android:name="com.example.app.ui.SettingsActivity"
        android:label="@string/title_settings"/>

    <action android:id="@+id/settingsAction"
        app:destination="@+id/settingsActivity"/>

</navigation>

并在NavigationView菜单中引用操作.

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    ...

    <group android:id="@+id/navigationGroupSettings">

        <item android:id="@+id/aboutAction"
            android:title="@string/title_about"
            android:icon="@drawable/ic_info_black_24dp"/>

        <item android:id="@+id/settingsAction"
            android:title="@string/title_settings"
            android:icon="@drawable/ic_settings_black_24dp"/>

    </group>

</menu>

这将显示SettingsActivity,但是当我按后退"按钮返回到MainActivity时,将显示第一个目的地,而不是先前的目的地.即使NavigationView正确保存了其状态,并检查了最后一个目的地(而不是列表中的第一项).

This displays the SettingsActivity, but when I press the back button to return to the MainActivity the first destination is displayed, rather than the previous destination. Even though the NavigationView saves its state correctly, and has the last destination checked (instead of the first item in the list).

我还尝试用SettingsFragment替换SettingsActivity,结果相同.

I also tried replacing the SettingsActivity with a SettingsFragment, with the same result.

在选择了全局操作后,如何使导航组件正确保存其状态?

How do I get the navigation component to correctly save its state after a global action is selected?

推荐答案

按照

默认情况下,后退堆栈将弹出回到导航图的起始目标位置.带有android:menuCategory="secondary"的菜单项不会弹出后退堆栈.

By default, the back stack will be popped back to the navigation graph's start destination. Menu items that have android:menuCategory="secondary" will not pop the back stack.

因此,如果要避免在选择设置目标位置时将其他目标位置弹出堆栈,则可以在菜单项中添加android:menuCategory="secondary".

Therefore you can add android:menuCategory="secondary" to your menu item if you'd like to avoid popping other destinations off the stack when you select your settings destination.

这篇关于导航视图全局操作返回状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆