【问题标题】:How to create BottomSheetDialogFragment using Navigation Architecture Component?如何使用导航架构组件创建 BottomSheetDialogFragment?
【发布时间】:2019-04-25 04:02:00
【问题描述】:

我使用 BottomSheetDialogFragment 来显示一些自定义设置。

要求:

当我单击 BottomSheetDialogFragment 中的任何选项卡时,我会替换片段并将其添加到 backstack,以便当用户单击 onBackPress 或 Up 操作时,它应该返回最后一个设置的 BottomSheetDialogFragment 片段。

我想使用导航架构组件来简化我的交易。

问题: 如果我使用导航架构组件从 FragmentA 导航到 BottomSheetDialogFragment,则会收到以下错误。

java.lang.IllegalStateException: 对话框不能为空 BottomSheetDialogFragment

我不知道如何使用导航架构组件来实例化 BottomSheetDialogFragment,并且使用下面的代码将不会使用导航架构组件来维护后台堆栈。

BottomSheetDialogFragment.show(FragmentManager manager, String tag)

【问题讨论】:

  • 你好@anmol 你有没有得到任何解决方案,因为我正在尝试做同样的事情
  • 我尝试用不同的方式来做这件事,看看这个演示项目github.com/andor201995/NavigationDemo@HemantSangle
  • 好的,我会检查链接
  • 我将构建一个正确的答案并在这里发布。 @HemantSangle
  • 那太好了

标签: android android-architecture-components bottom-sheet android-architecture-navigation


【解决方案1】:

在导航组件版本2.1.0-alpha04中,Navigation Graph可以包含dialog作为目的地之一。

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_navigation"
    app:startDestination="@id/startFragment">

    <fragment
        android:id="@+id/loginFragment"
        android:name="com.awesomeproject.android.authentication.login.LoginFragment"
        android:label="Login"
        tools:layout="@layout/login_fragment" />

    <dialog
        android:id="@+id/bottomSheet"
        android:name="com.awesomproject.android.BottomSheetFragment"
        tools:layout="@layout/bottom_sheet_dialog_fragment" />

</navigation>

BottomSheetFragment 看起来与其他 BottomSheet 相似。

class BottomSheetFragment : BottomSheetDialogFragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View =
            inflater.inflate(R.layout.bottom_sheet_dialog_fragment, container, false)
}

那么你可以像对待其他目的地一样对待bottomSheet。你可以导航到这个目的地或传递safeArgs in。

干杯!

【讨论】:

  • 我试过这个,但是当我尝试使用导航从对话框导航时,NavController 丢失了你可以分享从对话框到其他片段或对话框的导航代码吗? @Boonya
  • 使用此解决方案,底部工作表显示在另一个片段中,不会影响调用它的当前片段
  • 天哪,当我在全屏打开时添加带有导航的 BottomSheet 时,这很可怕。但是你节省了我的时间。从片段中更改对话框很容易。
  • @AndroLife 你知道这个问题的解决方案吗?
  • @AndroLife FWIW 我看到材料 1.4.0 和导航 2.3.5 的行为正确
猜你喜欢
  • 1970-01-01
  • 2021-11-08
  • 2019-09-23
  • 2018-12-22
  • 1970-01-01
  • 2019-03-29
  • 1970-01-01
  • 2018-12-03
  • 2019-12-21
相关资源
最近更新 更多