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, etcBaseFragment
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 Fragment
supportFragmentManager.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 method
gildor
02/10/2019, 12:17 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
setHasOptionsMenu
interface Rotatable {
fun rotate()
}
if (fragment is Rotatable) {
fragment.rotate()
}
...
toolbar.setTitleTextColor( fragment.titleColor )
fragment.setHasOptionsMenu(blah) // Yes! This will work anyway!
Reduce 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 thelayoutRes
BaseFragment
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 )