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)
The Navigation Component is made up of three major parts: Navigation Graph (New XML resource) — This is a resource that collects all navigation-related data in one place. This includes all of the locations in your app, referred to as destinations, as well as the possible paths a user could take through your app.
Create a navigation graph To add the graph, right-click on this directory, and choose New > Navigation resource file. The Navigation component uses an activity as a host for navigation and swaps individual fragments into that host as your users navigate through your app.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With