-
Notifications
You must be signed in to change notification settings - Fork 16
Navigate_DialogFragment
Add the annotation @Destination
to the DialogFragment
or BottomSheetDialogFragment
where navigation is required, and set the corresponding route path. Then call the navigate
method and pass in the destination path.
//Define destination
@Destination(route = Destinations.DESTINATION_TEST)
class TestDialogFragment : DialogFragment()
@Destination(BOTTOM_SHEET_DIALOG_FRAGMENT)
class TestBottomSheetDialogFragment : BottomSheetDialogFragment()
//Navigation
Butterfly.of(context).navigate(Destinations.DESTINATION_TEST)
The route path can be any string constant, and usually we use a format such as
scheme://path/ xxx
as the route address
DialogFragment navigation supports a variety of parameter transfer methods. Additional parameters can be passed in the form of concatenation paths, or parameters can be manually passed in by calling the params
method.
Either pass in parameters when calling the navigate method, or use a mixture of the above.
//Stitching path input parameters
Butterfly.of(this).navigate(Destinations.DESTINATION_TEST + "?a=1&b=2&c=3")
//Call the params method
Butterfly.of(this)
.params(
"x" to 1,
"y" to true,
"z" to "test value"
)
.navigate(Destinations.DESTINATION_TEST)
//Pass in parameters when calling navigate
Butterfly.of(this)
.navigate(
Destinations.DESTINATION_TEST,
"a" to "1"
)
//Use multiple ways at the same time
Butterfly.of(this)
.params(
"x" to 1,
"y" to true,
"z" to "test value"
)
.navigate(
Destinations.DESTINATION_TEST + "?a=1&b=2&c=3",
"d" to "4"
)
The priority of the above methods is: navigate > route string > params, that is, if you encounter parameters with the same name, parameters passed through the navigate method override parameters in the splice path string, which in turn override parameters passed through the params method.
The parameters passed in are packaged into a Bundle object and set to the Arguments of Fragment, so you only need to parse the Bundle object of Fragment to get the parameters.
@Destination(route = Destinations.DESTINATION_TEST)
class TestDialogFragment : DialogFragment() {
val a by params<String>()
val b by params<String>()
val c by params<String>()
}
After using Butterfly to navigate DialogFragment, if you need to return, you can use the popBack
method to fallback.
If you need to bring the return value with DialogFragment when fallback, you can pass the return value directly using popBack
.
@Destination(DIALOG_FRAGMENT)
class TestDialogFragment : DialogFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.back.setOnClickListener {
//Use popBack for fallback
Butterfly.of(context).popBack()
//Use popBack to fallback and return a value
Butterfly.of(context).popBack("result" to "123")
}
}
}
The returned result will be encapsulated as a Bundle
object, and the returned result can be obtained by parsing Bundle:
Butterfly.of(context).navigate(Destinations.DESTINATION_TEST) { result ->
if (result.isSuccess) {
val bundle = result.getOrDefault(Bundle.EMPTY)
val result by bundle.params<String>()
println(result)
}
}