DialogFragment #136
DAKolesnikov
started this conversation in
Ideas
Replies: 2 comments
-
Thank you for detail question! I like your points and will be think about support dialogs and other features. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Some thoughts about dialogs:
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Cicerone Version: 6.6
This discussion is a continuation of the issue DialogFragment support? #106
Sorry for long read)
We need dialogs
I agree with @terrakok's point of view. DialogFragmen are not navigation screen. Despite being called Fragmen, DialogFragmen has different interface.
But I disagree that Dialog should not be part of Cicerone. Look at it as a feature. Now I will try to explain my point of view.
For us - Android developers, Cicerone is a simple and powerful tool for app navigation from the ViewModel/Presenter. The library helps you forget about what you need "to keep in mind" all the time - Android Lifecycles.
Let's be honest, half of the opening and closing of screens occurs after asynchronous net response or cache saves events. Which are processed in the ViewModel/Presenter. Thus Cicerone becomes the core of the navigation layer.
Today, dialogs are increasingly used in the design of mobile app, in particular the material BottomSheetDialogFagment. They are used for product cards, share screens, option selection and ect, which contain actions. "Here's the design now do it". So to implement DialogFragment, you have to use WeakReferences, which look ugly next to Cicerone. I prefer more clean architecture in my code. So let's code it!
About DialogFragment
DialogFragment extends Fragment, which means that it can be used as a regular Fragment.
But if you use the show methods, Android put DialogFragment in window over Activity and show dialog.
Why should we betray FragmentManager? DialogFragment like other fragments is added to the FragmentManager list. You can check this by looking at
FragmentManager#fragments
after show dialog.However, the dialog is not added to the backstack. This is quite logical, because the dialogs have a slightly different back navigation. You can close the dialog in 3 ways:
onBackPressed
in the Activity. Realy convenient.It doesn't matter how the dialog is closed,
Dialog#dismiss()
will be called and FragmentManager will remove the fragment from the list. You can see this in the methodDialogFragment#dismissAllowingStateLoss
. This works becauseDialogFragment implements DialogInterface.OnDismissListener
and set itself into inner dialogmDialog.setOnCancelListener(this)
.Anyway for close DialogFragment the use the dismiss methods.
DialogFragment Cicerone integration
Create custom Screen
Сreate a custom screen to avoid unnecessary cast and not accidentally show the Activity as a dialog =)
FragmentScreen
class.createDialog(factory: FragmentFactory)
method that creates ourDialogFragment
. We also create an object in the constructor that creates a fragment for the FragmentScreen. You can not castdialogCreator
toCreator<FragmentFactory, Fragment>
.DialogScreen
in thenavigateTo
,replacesceen
, and other methods designed for fragment, becauseDialogScreen
extendsFragmentScreen
andDialogFragment
extendsFragment
and can be used as a regular fragment.As for me this code looks much better, but AppScreen is sealed.
Create custom Command and Router
Just commands for show and close dialogs .
If you need more flexible work with dialog methods. I suggest adding flags to the commands. Look at Forward command in Cicerone.
Create custom Navigator
Let's create our own Navigator in which we implement methods for dialogs.
AppNavigator
class.applyCommand
to handle dialog events.show
anddismiss
methods use attachedfragmentManager
.Screen#screenKey
like a tag in FragmentManger. Note that it is not nullUsage
In MyDialogFragment add DialogScreen.
In MainActivity use DialogRouter and DialogNavigator.
Some notes and questions for @terrakok
Why AppScreen is sealed?
I think creating custom screens will improve library usage. AppScreen - the navigation screen in the Android app. There are no others except Activity and Fragment.
In my project, I animated several fragments in the same way. I marked them with a special empty interface, which I checked in
setupFragmentTransaction
. But the problem is that this fragment will always have this animation. The best way to avoid this is to make an AnimatedScreen, where you can specify how to animate the fragment.Why don't you use
FragmentManager#addOnBackStackChangedListener
forlocalStackCopy
?That's all folks! Thanks for reading.
Beta Was this translation helpful? Give feedback.
All reactions