Android_800_thumb800

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
Fragments used for a tablet UI and for a smartphone UI

 

 

fragments_lifecycle
Fragment’s Life Cycle

Example 1: Shakespere Instrucment

android_fragment_landscape android_fragment_portait

/manifests/AndroidManifest.xml

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.

/res/layout/activity_main.xml

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

/res/layout-land/activity_main.xml

activity_main.xml for landscape view

/res/layout/detail.xml

MainActivity.java

DetailsFragment.java

Fragment

DetailsActivity.java

Use only for Portait view

TilesFragment.java

List fragment

Shakespere.java

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

http://developer.android.com/guide/components/fragments.html: The Android Developer’s Guide page to fragments.
http://developer.android.com/design/patterns/multi-pane-layouts.html: Android design guidelines for multipane layouts.
http://developer.android.com/training/basics/fragments/index.html: Android training page for fragments.

 

Android 6 – Fragments

Category: AndroidPrograming
0
460 views

Join the discussion

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

Comment moderation is enabled. Your comment may take some time to appear.