Fragments

Activities are used to build the different screens of an application. They can have UI components that they display to the screen using layouts and they can contain functional code that controls how different events cause different things to happen. While this model works really well for simple apps, it makes app growth painful if not impossible. If, for instance, you want to add a new feature to a single screen application, you'd have to alter all of the logic in the activity to accommodate this new feature. Then to add yet another feature, you'd have to alter all of that logic again. Constantly changing everything including the UI, code, data, and logic in your app's existing activities in order to introduce a new feature is not only inefficient, but it opens your app up to accidental bugs and errors.

How can we make our apps more flexible and more modular so that, if we want to add a new feature, we don't have to chop up our code every iteration? Android gave us the answer starting in Android 3.0 (API 11), Honeycomb. Google introduced the fragments API. Fragments are, as the name implies, a fragment of an application. Instead of having a single activity that contains all of the code and UI for several features, we can instead create multiple fragments, one for each feature. Each fragment could then contain its own UI and corresponding functionality code and act as its own separate module. When the application needs to use that feature, the fragment can then be added to the activity using a few short lines of code. Since we're making minimal changes to the activity, we're cutting down the chance for accidental bugs and we're making our code more modular.

References

http://developer.android.com/guide/components/fragments.html

results matching ""

    No results matching ""