问题
I am using BottomSheetDialogFragment for displaying few custom setting's.
Requirement:
When i click on any tab in BottomSheetDialogFragment i replace the fragment and add it to backstack so that when user click's onBackPress or Up action it should go back the the last setting's fragment of BottomSheetDialogFragment.
I want to use Navigation Architecture Component to simplify my transaction's.
Issue: if i use Navigation Architecture Component to navigate from FragmentA to BottomSheetDialogFragment then i receive below error.
java.lang.IllegalStateException: dialog must not be null BottomSheetDialogFragment
I don't know how to instantiate BottomSheetDialogFragment using Navigation Architecture Component and and using below code will not have a maintain backstack using Navigation Architecture Component.
BottomSheetDialogFragment.show(FragmentManager manager, String tag)
回答1:
In the navigation component version 2.1.0-alpha04
, Navigation Graph
can contain dialog
as one of the destinations.
<?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>
The BottomSheetFragment will look similar to other BottomSheet.
class BottomSheetFragment : BottomSheetDialogFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View =
inflater.inflate(R.layout.bottom_sheet_dialog_fragment, container, false)
}
Then you can treat bottomSheet
the same way as other destinations. You can navigate to this destination or passing safeArgs
in.
Cheers!
来源:https://stackoverflow.com/questions/53431473/how-to-create-bottomsheetdialogfragment-using-navigation-architecture-component