headless fragments in android

10
Headless Fragments in Android By Ali Muzaffar

Upload: ali-muzaffar

Post on 21-Mar-2017

1.085 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Headless fragments in Android

Headless Fragments in Android

By Ali Muzaffar

Page 2: Headless fragments in Android

Strategies for code reuse

Helper classesBase Activity

Page 3: Headless fragments in Android

Helper classes/methods

Pro: Self contained. Helper classes either contain static methods related to certain

functionality such as Networking or are wholly dedicated to doing one thing.

Pro: Easy to modify without risk of breaking. If you want to change a static method, it's easy to see who all is

calling the method and what the impact may be, worst case is that you overload the current method or create a new method.

Con: Is not aware of the Activity lifecycle. Lot more code to handle lifecycle events. Risk of memory leaks.

Page 4: Headless fragments in Android

Inherit from parent Activity Pro: Aware of lifecycle events. Pro: ContextWrapper, no need to pass around

context. Con: Very rigid implementation

All children get every feature, disabling features requires more code or flags and this make the code very non-intuitive to read.

Can't inherit from anything else like PreferenceActivity, ListActivity etc.

Hard to handle long running events across orientation changes.

Page 5: Headless fragments in Android

The age old battle of Composition versus Inheritance Composition over inheritance (or Composite Reuse Principle)

in object-oriented programming is the principle that classes should achieve polymorphic behavior and code reuse by composition (containing other classes that implement the desired functionality), instead of through inheritance (being a subclass). - Kirk Knoernschild (2002). Java Design - Objects, UML, and Process: 1.1.5 Composite Reuse Principle (CRP).

"has a" versus "is a".

Ignore the internet, eachhas it's merits

Page 6: Headless fragments in Android

Strategies for code reuse

Helper classesBase ActivityHeadless Fragment

Page 7: Headless fragments in Android

FRAGMENTS, OFF WITH THEIR HEADS!!!

Page 8: Headless fragments in Android

What are headless Fragments Headless Fragments are regular fragments that return null in

onCreateView() or do not override it.

They *often* have setRetainInstance() set to true in onCreate().

In every other way, they are just a Fragment. You get the convenience of a helper class and all the power of a Fragment (lifecycle and context awareness).

They use Composition to provide functionality to your Activity or Fragments.

Page 9: Headless fragments in Android

Examples of how to create headless Fragments NetworkHelper

A headless Fragment to check for connectivity to the internet.

CameraMicPermissionHelper A headless Fragment that requests Android runtime permissions.

Read: Using headless Fragments to check network connectivity and request Android M Runtimer permissions on Medium or LinkedIn

Page 10: Headless fragments in Android

How do I use a headless FragmentPretty much the same way as using a regular fragment. Since you retain instance, you want to check if an instance already exists and if not, create one and attach it.