【问题标题】:Up button navigation with multiple top destinations and subgraphs -- all children of subgraph are also top level具有多个顶级目的地和子图的向上按钮导航——子图的所有子级也是顶级的
【发布时间】:2021-05-11 23:04:50
【问题描述】:

导航图:

<?xml version="1.0" encoding="utf-8"?>
<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/mobile_navigation"
    app:startDestination="@+id/navigation_home"
    >

    <fragment
        android:id="@+id/navigation_home"
        android:name="com.hanafey.android.waterstats.ui.home.HomeFragment"
        android:label="@string/title_home"
        tools:layout="@layout/fragment_home"
        />

    <!-- ......   -->

    <navigation
        android:id="@+id/navigation_graph_setup"
        android:label="Setup"
        app:startDestination="@id/navigation_setup"
        >
        <fragment
            android:id="@+id/navigation_setup"
            android:name="com.hanafey.android.waterstats.ui.setup.SetupFragment"
            android:label="@string/title_setup"
            tools:layout="@layout/fragment_setup"
            />
        <fragment
            android:id="@+id/navigation_find_or_create_results_sheet"
            android:name="com.hanafey.android.waterstats.ui.setup.FindOrCreateResultsSheetFragment"
            android:label="Find Or Create Results Sheet"
            tools:layout="@layout/fragment_find_or_create_results_sheet"
            />
        <fragment
            android:id="@+id/navigation_show_results_sheet_details"
            android:name="com.hanafey.android.waterstats.ui.setup.FindOrCreateResultsSheetDetailsFragment"
            android:label="Find Or Create Result Sheet Details"
            tools:layout="@layout/fragment_file_find_or_initialize_result"
            />
    </navigation>
</navigation>

操作栏设置如下:

val topLevelDid = setOf(
    R.id.navigation_home,
    R.id.navigation_septic,
    R.id.navigation_notifications,
    R.id.navigation_graph_setup,
)
appBarConfiguration = AppBarConfiguration(topLevelDid)
setupActionBarWithNavController(navController, appBarConfiguration)

问题是子图R.id.navigation_graph_setup 的所有成员都变为顶级,并且没有显示向上按钮。期望的行为只是子图的起始目的地是顶层。

【问题讨论】:

    标签: android android-architecture-navigation


    【解决方案1】:

    答案很简单,不要将子图标记为顶层。而是将子图的起始目的地标记为顶层。这意味着您需要在图形 xml 和代码中保持一致。如果在代码中将子图指定为 top 而不是将起始目的地设置为 top 会更好,但是如果在运行时更改起始目的地,这会很复杂。

    “androidx.navigation.ui.AbstractAppBarOnDestinationChangedListener”中的代码显示了如何通过查看目的地然后递归查看其所有父级来推断顶层。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-09
      • 1970-01-01
      • 1970-01-01
      • 2021-09-23
      • 2014-10-04
      • 1970-01-01
      • 1970-01-01
      • 2015-05-21
      相关资源
      最近更新 更多