Android 具有多个顶级目的地和back key行为的导航图

Android 具有多个顶级目的地和back key行为的导航图,android,android-architecture-navigation,android-navigation-graph,Android,Android Architecture Navigation,Android Navigation Graph,当我在导航图中有多个顶级目的地,并且当我单击后退键完成我的应用程序时,除了startDestination之外的那些目的地不会完成,而是弹出startDestination片段 FragmentA(startDestination)->使用back键立即完成 FragmentB->使用back键返回FragmentA FragmentC->使用back键返回FragmentA 我尝试了app:popUpTo=“@id/nav_graph”和/或app:popuptinclusive=“tru

当我在导航图中有多个顶级目的地,并且当我单击后退键完成我的应用程序时,除了
startDestination
之外的那些目的地不会完成,而是弹出
startDestination
片段

  • FragmentA(startDestination)->使用back键立即完成
  • FragmentB->使用back键返回FragmentA
  • FragmentC->使用back键返回FragmentA
我尝试了
app:popUpTo=“@id/nav_graph”
和/或
app:popuptinclusive=“true”
,但没有成功

活动:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);

        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);

        DrawerLayout drawerLayout = findViewById(R.id.drawer_layout);

        AppBarConfiguration appBarConfiguration =
                new AppBarConfiguration.Builder(R.id.fragmentA, R.id.fragmentB, R.id.fragmentC)
                        .setOpenableLayout(drawerLayout)
                        .build();

        NavigationView navView = findViewById(R.id.nav_view);
        NavigationUI.setupWithNavController(navView, navController);

        Toolbar toolbar = findViewById(R.id.toolbar);
        NavigationUI.setupWithNavController(
                toolbar,
                navController,
                appBarConfiguration
        );
    }
}
<?xml version="1.0" encoding="utf-8"?>
<!-- Use DrawerLayout as root container for activity -->
<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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <!-- Layout to contain contents of navigation_view body of screen (drawer will slide over this) -->
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/AppTheme.AppBarOverlay"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

        <fragment
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:defaultNavHost="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/toolbar"
            app:navGraph="@navigation/nav_graph" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <!-- Container for contents of drawer - use NavigationView to make configuration easier -->
    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/navigation_header"
        app:menu="@menu/navigation_view" />

</androidx.drawerlayout.widget.DrawerLayout>
<?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"
    android:id="@+id/nav_graph"
    app:startDestination="@id/fragmentA">

    <fragment
        android:id="@+id/fragmentA"
        android:name="com.stackoverflow.FragmentA"
        android:label="@string/fragmentA">
        <argument
            android:name="message"
            android:defaultValue="(no message)"
            app:argType="string" />
    </fragment>

    <action
        android:id="@+id/toFragmentAWithMessage1"
        app:destination="@id/fragmentA">
        <argument
            android:name="message"
            android:defaultValue="message 1"
            app:argType="string" />
    </action>

    <action
        android:id="@+id/toFragmentAWithMessage2"
        app:destination="@id/fragmentA">
        <argument
            android:name="message"
            android:defaultValue="message 2"
            app:argType="string" />
    </action>

    <fragment
        android:id="@+id/fragmentB"
        android:name="com.stackoverflow.navigationui.FragmentB"
        android:label="@string/fragmentB">
        <argument
            android:name="message"
            android:defaultValue="(no message)"
            app:argType="string" />
    </fragment>

    <action
        android:id="@+id/toFragmentBWithMessage1"
        app:destination="@id/fragmentB">
        <argument
            android:name="message"
            android:defaultValue="message 1"
            app:argType="string" />
    </action>

    <action
        android:id="@+id/toFragmentBWithMessage2"
        app:destination="@id/fragmentB">
        <argument
            android:name="message"
            android:defaultValue="message 2"
            app:argType="string" />
    </action>

    <fragment
        android:id="@+id/fragmentC"
        android:name="com.stackoverflow.FragmentC"
        android:label="@string/fragmentC">
        <argument
            android:name="message"
            android:defaultValue="(no message)"
            app:argType="string" />
    </fragment>

    <action
        android:id="@+id/toFragmentCWithMessage1"
        app:destination="@id/fragmentC">
        <argument
            android:name="message"
            android:defaultValue="message 1"
            app:argType="string" />
    </action>

    <action
        android:id="@+id/toFragmentCWithMessage2"
        app:destination="@id/fragmentC">
        <argument
            android:name="message"
            android:defaultValue="message 2"
            app:argType="string" />
    </action>

</navigation>
layout.xml:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);

        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);

        DrawerLayout drawerLayout = findViewById(R.id.drawer_layout);

        AppBarConfiguration appBarConfiguration =
                new AppBarConfiguration.Builder(R.id.fragmentA, R.id.fragmentB, R.id.fragmentC)
                        .setOpenableLayout(drawerLayout)
                        .build();

        NavigationView navView = findViewById(R.id.nav_view);
        NavigationUI.setupWithNavController(navView, navController);

        Toolbar toolbar = findViewById(R.id.toolbar);
        NavigationUI.setupWithNavController(
                toolbar,
                navController,
                appBarConfiguration
        );
    }
}
<?xml version="1.0" encoding="utf-8"?>
<!-- Use DrawerLayout as root container for activity -->
<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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <!-- Layout to contain contents of navigation_view body of screen (drawer will slide over this) -->
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/AppTheme.AppBarOverlay"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

        <fragment
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:defaultNavHost="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/toolbar"
            app:navGraph="@navigation/nav_graph" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <!-- Container for contents of drawer - use NavigationView to make configuration easier -->
    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/navigation_header"
        app:menu="@menu/navigation_view" />

</androidx.drawerlayout.widget.DrawerLayout>
<?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"
    android:id="@+id/nav_graph"
    app:startDestination="@id/fragmentA">

    <fragment
        android:id="@+id/fragmentA"
        android:name="com.stackoverflow.FragmentA"
        android:label="@string/fragmentA">
        <argument
            android:name="message"
            android:defaultValue="(no message)"
            app:argType="string" />
    </fragment>

    <action
        android:id="@+id/toFragmentAWithMessage1"
        app:destination="@id/fragmentA">
        <argument
            android:name="message"
            android:defaultValue="message 1"
            app:argType="string" />
    </action>

    <action
        android:id="@+id/toFragmentAWithMessage2"
        app:destination="@id/fragmentA">
        <argument
            android:name="message"
            android:defaultValue="message 2"
            app:argType="string" />
    </action>

    <fragment
        android:id="@+id/fragmentB"
        android:name="com.stackoverflow.navigationui.FragmentB"
        android:label="@string/fragmentB">
        <argument
            android:name="message"
            android:defaultValue="(no message)"
            app:argType="string" />
    </fragment>

    <action
        android:id="@+id/toFragmentBWithMessage1"
        app:destination="@id/fragmentB">
        <argument
            android:name="message"
            android:defaultValue="message 1"
            app:argType="string" />
    </action>

    <action
        android:id="@+id/toFragmentBWithMessage2"
        app:destination="@id/fragmentB">
        <argument
            android:name="message"
            android:defaultValue="message 2"
            app:argType="string" />
    </action>

    <fragment
        android:id="@+id/fragmentC"
        android:name="com.stackoverflow.FragmentC"
        android:label="@string/fragmentC">
        <argument
            android:name="message"
            android:defaultValue="(no message)"
            app:argType="string" />
    </fragment>

    <action
        android:id="@+id/toFragmentCWithMessage1"
        app:destination="@id/fragmentC">
        <argument
            android:name="message"
            android:defaultValue="message 1"
            app:argType="string" />
    </action>

    <action
        android:id="@+id/toFragmentCWithMessage2"
        app:destination="@id/fragmentC">
        <argument
            android:name="message"
            android:defaultValue="message 2"
            app:argType="string" />
    </action>

</navigation>

有更基本的解决方案吗?

只需删除或设置layout.xml中NavHostFragment的
属性的
app:defaultNavHost

<fragment
    android:id="@+id/nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    app:defaultNavHost="false"
    />

请注意,
setPrimaryNavigationFragment(finalHost)
允许您的NavHost 拦截系统返回按钮按下。您也可以实现这一点 通过添加
app:defaultNavHost=“true”
在NavHost XML中的行为

将此FragmentManager中当前活动的片段设置为主导航片段

将调用主导航片段的子FragmentManager 首先处理委派的导航操作,例如
FragmentManager.popBackStack()
如果未指定ID或事务名称 提供给流行的。片段外部的导航操作 系统可以选择将这些操作委派给主导航 返回的片段
FragmentManager.getPrimaryNavigationFragment()

由于您将app:defaultNavHost设置为true
app:defaultNavHost
,因此您的NavHost会截获后退键并导致弹出startDestination片段a