What is the advantage to using Fragment
s over using custom View
s that are reused in different layouts?
In the original blog post introducing fragments, Dianne Hackborn says that
[Fragments] make it easier for developers to write applications that can scale across a variety of screen sizes, beyond the facilities already available in the platform.
and she goes on to explain Fragments in the context of making a tablet layout for an app that combines the UI of two activities from the phone version of the same app.
But it seems that the same reuse could be achieved using custom Views. The main different between Fragments and Views seems to be that they have differing lifecycles...
The Fragment
lifecycle is:
onAttach()
, onCreate()
, onCreateView()
, onActivityCreated()
, onStart()
, onResume()
, onPause()
, onStop()
, onDestroyView()
, onDestroy()
, onDetatch()
.
The View
lifecycle is:
ctor
, onFinishInflate()
, onAttachedToWindow()
, onMeasure()
, onLayout()
, onDetatchedFromWindow()
I'd like to hear from developers with experience writing large apps about what benefits (if any) they've seen in using Fragments vs custom Views to divide up the UI into reusable pieces.