Davide Giuseppe Farella
02/09/2019, 4:39 AMsealed class be a good option for an abstract Fragment?
sealed class BaseFragment: Fragment()
abstract class RootFragment: BaseFragment()
abstract class NestedFragment: BaseFragment()gildor
02/09/2019, 6:44 AMDavide Giuseppe Farella
02/09/2019, 11:39 AMBaseFragment has ( had, no RootFragmant has ) many, like title, titleColor, etcDavide Giuseppe Farella
02/09/2019, 11:42 AMBaseFragment everywhere and 2 Fragment in my codebaserkeazor
02/09/2019, 1:30 PMDavide Giuseppe Farella
02/09/2019, 1:33 PMgildor
02/10/2019, 9:10 AMDavide Giuseppe Farella
02/10/2019, 9:51 AMlayoutRes and inflate the View only in the BaseFragment
Using an interface would make sense if I want to define a specific behaviour for some Fragments: e.g. SomeFrag: Fragment(), RotationEnabled -> if ( fragment is RotationEnabled ) fragment.rotate(), in that case I wonāt care to have a āsolid distinctionā between a RotationEnabled and a FragmentDavide Giuseppe Farella
02/10/2019, 9:58 AMsupportFragmentManager.onFragmentResumed {
val fragment = it as BaseFragment // I don't really do that in this way, it's just for explanation purpose :D
fragment.title?.let { title = it }
toolbar.setTitleTextColor( fragment.titleColor )
fragment.setHasOptionsMenu( fragment.menuRes != null )
setBackgroundColor( fragment.backgroundColor ?: getThemeColor( android.R.attr.colorBackground ) )
}
Using an interface Iāll end up in this
val fragment = it as BaseFragment
...
toolbar.setTitleTextColor( fragment.titleColor )
fragment.setHasOptionsMenu // Dho! BaseFragment is not a Fragment so it doesn't have this methodDavide Giuseppe Farella
02/10/2019, 10:01 AMgildor
02/10/2019, 12:17 PMgildor
02/10/2019, 12:18 PMfragment.setHasOptionsMenu // Dho! BaseFragment is not a Fragment so it doesnāt have this method```Not sure what you mean, because any fragment has method
setHasOptionsMenugildor
02/10/2019, 12:19 PMinterface Rotatable {
fun rotate()
}gildor
02/10/2019, 12:21 PMif (fragment is Rotatable) {
fragment.rotate()
}
...
toolbar.setTitleTextColor( fragment.titleColor )
fragment.setHasOptionsMenu(blah) // Yes! This will work anyway!gildor
02/10/2019, 12:22 PMReduce some verbosity, e.g. define aBut how this related to Nested/Root fragment? Iām not strongly against BaseFragment in general, Iām talking about your case with sealed classand inflate the View only in thelayoutResBaseFragment
Davide Giuseppe Farella
02/10/2019, 1:13 PMtitleColorRes is a property of my RootFragmant š
All described above is not strictly related to sealed class, sealed class is a side-effect of my current use case. Usually I only use that BaseFragment without nesting Fragment, but now I want a Fragment to have 2 nested Frags and, since nested Frags must NOT have params such title or options menu, I've choose to "transform" BaseFragment in RootFragmant and use BaseFragment as an abstraction layer suitable from boot root Frags and nested Frags and, since I won't other types of Frags, I wanted to make BaseFragment "sealed" and I thought to use sealed class instead of an abstract class with a protected constructor.
So basically, before:
* BaseFragment ( frags behaviors and params such as title color, etc )
after:
* BaseFragment ( only common behaviors and params suitable for both types )
* RootFragmant ( params for root Fragments )
* NestedFragment ( actually basically a BaseFragment, maybe will implement behaviors for only nested Fragment,Ike parentFragment - i won't do that, just for give an idea )Davide Giuseppe Farella
02/10/2019, 1:13 PM