Android导航架构组件:错误的BackStack与NavigationView

时间:2018-10-18 08:15:50

标签: android navigationview androidx android-architecture-navigation

我正在使用Android Navigation Architecture Components来导航基于片段的单一活动应用程序。主要导航是通过NavigationDrawer模式实现的:DrawerLayout带有NavigationView和NavHostFragment以及其他工具栏。

到目前为止,一切工作正常-除主级Fragments的BackStack外,我能够导航到每个Fragment-。除了startDestination以外的任何Fragment似乎都已添加到BackStack中,而不是像常规NavigationDrawer那样被替换。

当我导航到startDestination以外的Fragment时,可以通过Burger-icon(切换到Back-icon)来识别不当行为,并通过向后导航将其引导到startDestination而不退出应用程序。< / p>

应如何使用Navigation Architecture组件,以便与NavigationView一起替换主要片段?

我正在初始化导航架构组件,如下所示:

class MainActivity : AppCompatActivity {

    private val navigationHostFragment: NavHostFragment
        get() = supportFragmentManager.findFragmentById(R.id.navigationHost) as NavHostFragment

    private val navigationController: NavController
        get() = navigationHostFragment.navController

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

        // Views are bound via Kotlin Android Extensions
        setSupportActionBar(toolbar)
        NavigationUI.setupWithNavController(toolbar, navigationController, drawerLayout)
        NavigationUI.setupWithNavController(navigationView, navigationController)
    }

    override fun onSupportNavigateUp(): Boolean = findNavController(R.id.navigationHost).navigateUp()
}

活动(/res/layout/activity.xml)的布局如下:

<androidx.drawerlayout.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"/>

        <fragment
            android:id="@+id/navigationHost"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:navGraph="@navigation/navigation"
            app:defaultNavHost="true"/>

    </LinearLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigationView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/navigation"/>

</androidx.drawerlayout.widget.DrawerLayout>

导航图(/res/navigation/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"
    app:startDestination="@id/first">

    <fragment
        android:id="@+id/first"
        android:name="de.example.FirstFragment"
        android:label="@string/first"/>

    <fragment
        android:id="@+id/second"
        android:name="de.example.SecondFragment"
        android:label="@string/second"/>

</navigation>

NavigationView(/res/menu/navigation.xml)的菜单如下:

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

    <group android:checkableBehavior="single">

        <item
            android:id="@+id/first"
            android:title="@string/first"/>

        <item
            android:id="@+id/second"
            android:title="@string/second"/>

    </group>

</menu>

相关的依赖项如下:

dependencies {
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.core:core-ktx:1.0.0'
    implementation 'android.arch.navigation:navigation-fragment-ktx:1.0.0-alpha06'
    implementation 'android.arch.navigation:navigation-ui-ktx:1.0.0-alpha06'
}

1 个答案:

答案 0 :(得分:0)

Alex posted一样,我最初的目标与Principles of Navigation冲突:

  

应用具有固定的目的地,即用户从启动器启动应用时看到的屏幕。此目的地也应该是用户在按下后退按钮后返回启动器时看到的最后一个屏幕。