【问题标题】:Navigation Architecture Component: How i can navigate a fragment from the back stack?导航架构组件:如何从后台堆栈导航片段?
【发布时间】:2019-01-18 20:35:19
【问题描述】:

我有两个片段 A 和 B 我正在使用导航控制器从 A 导航到 B 当我回到 A 时导航控制器创建另一个 A 实例!不从堆栈中调用旧实例并增加 sack 大小,在这种情况下,堆栈大小将为 3 个片段, 我该如何解决这个问题?

这是我的graph.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"
android:id="@+id/home_dest"
app:startDestination="@id/home_dest">

<fragment
    android:id="@+id/a_dest"
    android:name="com.intcore.e_commerce.e_commerce.ui.homefragment.HomeFragment"
    android:label="@string/home"
    tools:layout="@layout/fragment_home" />
<fragment
    android:id="@+id/b_dest"
    android:name="com.intcore.e_commerce.e_commerce.ui.favoritesfragment.FavoritesFragment"
    android:label="@string/favorites"
    tools:layout="@layout/fragment_favorites" /></avigation>

这是我用来导航的代码。

void onAClicked() {
    Navigation.findNavController(this, R.id.nav_host_fragment).navigate(R.id.a_dest);
}


void onBClicked() {
    Navigation.findNavController(this, R.id.nav_host_fragment).navigate(R.id.b_dest);
}

【问题讨论】:

  • 我想onAClicked在B片段上;那么为什么不从onAClicked 调用Navigation.findNavController(requireActivity(), R.id.navHost).navigateUp() 呢?

标签: android android-studio android-fragments android-intent


【解决方案1】:
  1. 对于将 nav_host_fragment 连接到 a_dest 的操作,您需要将弹出行为设置为 popTo nav_host_fragment inclusive(在导航编辑器中)。
  2. 重复相同的操作将 nav_host_fragment 连接到 b_dest。 PopTo inclusive 会弹出回栈中的所有内容,包括引用的片段事务。
  3. 创建一个将 a_dest 连接到 nav_host_fragment 以及从 b_dest 连接到 nav_host_fragment 的操作。对于每个片段,选择 PopTo nav_host_fragment 并关闭包含标志。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-30
    • 2018-12-07
    • 2018-11-04
    • 1970-01-01
    • 2018-10-23
    相关资源
    最近更新 更多