You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've noticed boilerplate code when overriding lifecycle methods: we have to call parent method for each override (call to super).
Some developers don't follow best practices for when to call super (i.e. is it before or after setup/cleanup code).
It's possible to enforce those rules and remove call to super from Fragments by creating BaseFragment that would:
extend Fragment and override lifecycle methods,
make overrides final so that they can't be overridden in Fragments,
declare setup/cleanup methods: protected open fun do[lifecycleMethodName] that we can override in Fragments,
call setup/cleanup methods at correct locations in overrides.
This is inspired by template method design pattern.
Correct locations of setup/cleanup method calls:
onCreate, onViewCreated, onStart, onResume - after call to super
onPause, onStop, onDestroy, onDestroyView - before call to super.
Example of that kind of method in BaseFragment:
final override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
doOnViewCreated(view, savedInstanceState)
}
protected open fun doOnViewCreated(view: View, savedInstanceState: Bundle?) {}
For example when we want to override onViewCreated in a Fragment we would override doOnViewCreated instead.
Add clear warning to Kdoc of BaseFragment that it should be lightweight and it's not intended to hold methods that could be extension functions. We don't want another View superclass.
The text was updated successfully, but these errors were encountered:
I think what you propose is good! I am also one of those who use a BaseFragment/BaseActivity to reduce the boilerplate code.
We just need to be careful to not add too many things in the Base classes.
Uh oh!
There was an error while loading. Please reload this page.
I've noticed boilerplate code when overriding lifecycle methods: we have to call parent method for each override (call to
super
).Some developers don't follow best practices for when to call
super
(i.e. is it before or after setup/cleanup code).It's possible to enforce those rules and remove call to
super
fromFragments
by creatingBaseFragment
that would:Fragment
and override lifecycle methods,final
so that they can't be overridden inFragments
,protected open fun do[lifecycleMethodName]
that we can override inFragments
,This is inspired by template method design pattern.
Correct locations of setup/cleanup method calls:
onCreate
,onViewCreated
,onStart
,onResume
- after call tosuper
onPause
,onStop
,onDestroy
,onDestroyView
- before call tosuper
.Example of that kind of method in
BaseFragment
:For example when we want to override
onViewCreated
in aFragment
we would overridedoOnViewCreated
instead.Add clear warning to Kdoc of
BaseFragment
that it should be lightweight and it's not intended to hold methods that could be extension functions. We don't want another View superclass.The text was updated successfully, but these errors were encountered: