我在用BottomSheetDialogFragment用于显示一些自定义设置。
要求:
当我单击 BottomSheetDialogFragment 中的任何选项卡时,我会替换该片段并将其添加到后台堆栈,以便当用户单击 onBackPress 或 Up 操作时,它应该返回 BottomSheetDialogFragment 的最后一个设置的片段。
我想使用导航架构组件来简化我的事务。
问题:
如果我使用导航架构组件从 FragmentA 导航到 BottomSheetDialogFragment,那么我会收到以下错误。
java.lang.IllegalStateException:对话框不能为空
BottomSheetDialogFragment
我不知道如何使用导航架构组件实例化 BottomSheetDialogFragment,并且使用下面的代码将不会使用导航架构组件维护后台堆栈。
BottomSheetDialogFragment.show(FragmentManager manager, String tag)
在导航组件版本中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.
Cheers!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)