One way to think of a fragment is as a sub-activity. And in fact, the semantics of a fragment are a lot like an activity. A fragment can have a view hierarchy associated with it, and it has a life cycle much like an activity’s life cycle.

Fragments can even respond to the Back button like activities do. If you were thinking, “If only I could put multiple activities together on a tablet’s screen at the same time,” then you’re on the right track. But because it would be too messy to have more than one activity of an application active at the same time on a tablet screen, fragments were created to implement basically that thought. This means fragments are contained within an activity. Fragments can only exist within the context of an activity; you can’t use a fragment without an activity.

Fragments can coexist with other elements of an activity, which means you do not need to convert the entire user interface of your activity to use fragments. You can create an activity’s layout as before and only use a fragment for one piece of the user interface. Fragments are not like activities, however, when it comes to saving state and restoring it later. The fragments framework provides several features to make saving and restoring fragments much simpler than the work you need to do on activities

Fragments used for a tablet UI and for a smartphone UI



Fragment’s Life Cycle

Example 1: Shakespere Instrucment

android_fragment_landscape android_fragment_portait


Config DetailActivity action and category. This allows you to start the details activity from code but will not show the details activity as an app in the App list.


activity_main.xml for all view (in this context only portait)


activity_main.xml for landscape view



Use only for Portait view

List fragment

Static data for demo

That although fragments can be instantiated with a static factory method such as newInstance(), you must always have a default constructor and a way to save initialization values into an initialization arguments bundle.

Using fragment transactions to change what’s displayed to a user, and animating those transitions using cool effects.

References The Android Developer’s Guide page to fragments. Android design guidelines for multipane layouts. Android training page for fragments.


Android 6 – Fragments

Category: AndroidPrograming

Join the discussion

Your email address will not be published. Required fields are marked *