【问题标题】:Navigation graph with multiple top level destinations and back-key behaviors具有多个顶级目的地和后键行为的导航图
【发布时间】:2021-02-04 07:15:27
【问题描述】:

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

  • FragmentA (startDestination) -> 使用返回键立即结束
  • FragmentB -> 使用返回键返回 FragmentA
  • FragmentC -> 使用返回键返回 FragmentA

我尝试过app:popUpTo="@id/nav_graph" 和/或app:popUpToInclusive="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
        );
    }
}

layout.xml:

<?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>

nav_graph:

<?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>

我知道覆盖 OnBackPressedCallback 是一种解决方法。

public class FragmentB extends Fragment {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        requireActivity().getOnBackPressedDispatcher().addCallback(new OnBackPressedCallback(true) {
            @Override
            public void handleOnBackPressed() {
                remove();
                requireActivity().finish();
            }
        });
    }
}

有没有更根本的解决方案?

【问题讨论】:

    标签: android android-architecture-navigation android-navigation-graph


    【解决方案1】:

    只需将 false 删除或设置为 layout.xml 中 NavHostFragment 的 &lt;fragment&gt;app:defaultNavHost 属性即可:

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

    Interact programmatically with the Navigation component

    请注意,setPrimaryNavigationFragment(finalHost) 允许您的 NavHost 拦截系统后退按钮按下。你也可以实现这个 通过添加 app:defaultNavHost="true" 在您的 NavHost XML 中的行为。

    setPrimaryNavigationFragment

    在这个 FragmentManager 中设置一个当前活动的片段作为主导航片段。

    主导航片段的子 FragmentManager 将被调用 首先处理委派的导航操作,例如 FragmentManager.popBackStack() 如果没有 ID 或事务名称 提供弹出。片段外的导航操作 系统可以选择将这些操作委托给主导航 由返回的片段 FragmentManager.getPrimaryNavigationFragment().

    由于您将 app:defaultNavHost 设置为 true,您的 NavHost 会拦截返回键并导致弹出 startDestination FragmentA。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-09
      • 1970-01-01
      • 1970-01-01
      • 2019-05-29
      • 2018-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多