【问题标题】:Handle navigation stack in NavController navigation graph android?在NavController导航图android中处理导航堆栈?
【发布时间】:2020-06-08 09:55:08
【问题描述】:

工具栏上有一个设置按钮,可以打开一个片段,其中包含打开更多片段的选项列表。

设置页面有 2 个选项

  • 简介
  • 更改密码

用户可以导航到任何页面,并且工具栏对每个页面的用户都是可见的

如果我单击配置文件 -> 启动配置文件片段 -> 然后我单击工具栏上的设置 -> 启动设置页面

现在,当我按下返回时,我被重定向到我不希望发生的配置文件片段。 它应该重定向到设置片段之前访问的最后一个页面,因为配置文件和密码片段是设置片段的子片段

这是我的设置片段流导航图

 <fragment
    android:id="@+id/settingsFragment"
    android:name="com.mountmeru.view.settings.SettingsFragment"
    android:label="fragment_settings"
    tools:layout="@layout/fragment_settings">
    <action
        android:id="@+id/action_settingsFragment_to_profileFragment"
        app:destination="@id/profileFragment" />
    <action
        android:id="@+id/action_settingsFragment_to_resetPasswordFragment"
        app:destination="@id/resetPasswordFragment" />
</fragment>

这就是我导航到个人资料和密码片段的方式

 view.findNavController().navigate(
        R.id.action_settingsFragment_to_profileFragment)

 view.findNavController().navigate(
        R.id.action_settingsFragment_to_resetPasswordFragment)

【问题讨论】:

    标签: android navigation fragment android-architecture-navigation android-jetpack-navigation


    【解决方案1】:

    在 ProfileFragment 的导航图中你可以使用:

    <fragment
        android:id="@+id/profileFragment"            
        ...>
    
        <action
            android:id="@+id/action_profileFragment_to_settingsFragment"
            app:destination="@+id/settingsFragment"
            app:popUpTo="@id/profileFragment"
            app:popUpToInclusive="true" />
    
    </fragment>
    

    您可以找到有关 popUpTo 和 pupUpToInclusive here 的更多信息。

    你也可以在代码中做(不改变导航图):

    findNavController()
            .navigate(R.id.action_profileFragment_to_settingsFragment,
                    null,
                    NavOptions.Builder()
                        .setPopUpTo(R.id.profileFragment,
                         true).build()
            )
    

    【讨论】:

      猜你喜欢
      • 2020-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多