Activities vs Fragments. Every Android developer ever who wasn¡¯t forced to use a specific view container had to dabble with this question for a bit before choosing one. Is it better to go with an all-activity build to cut down on the number of lifecycle events I have to keep in mind and work with, or do I go with an all-fragment build to better prepare myself when I enter tablet land? Well, this presentation throws out some food for thought for the readers to help them better decide which is the better route to take of the two or, perhaps if there is a better third route that combines the best of both worlds.
2. ? A look at how we (used to) do things
? Comparison between coding styles
? Building for modularity and testability
? Pros and cons with an all Activity build
? Pros and cons with an all Fragment build
? Proposing a yet-to-be-tested-enough solution
Overview
26. Pros of an all Activity build
? Activities mainly act as view controllers, which allows
them to hold their own state (Context).
? Testing Activities is much easier than Fragments
? Back-stack handling is done by the framework in the
background
27. Cons of an all Activity build
? Activities mainly act as view controllers, which allows
them to hold their own state (Context)
? Testing Activities is much easier than Fragments
? The views created in Activities may not always be the
best suited views for different sizes of screens
? Activities can¡¯t be reused as a whole
28. Pros of an all Fragment build
? Fragments, by design, are made to be reusable
components
? They are the ¡°only¡± way to optimize our apps¡¯ UI to fit
bigger screens
29. Cons of an all Fragment build
? Complicated life-cycle events that are already nested
inside the Activity¡¯s life-cycle events
? Fragments can¡¯t communicate amongst each other
unless it is through an Activity
? Testing Fragments requires the presence of the
Activity¡¯s Context.
? Handling the changes in the back-stack must be done
manually