Creating fragments
As with activities, now that we have covered the same old dry introduction to fragments, we can actually begin to use them! In the following sections, we will cover how to create fragments, create a static Fragment
constructor, and instantiate a fragment that binds one or several fragments into a single parent activity.
Creating our own fragment
As we did in the activity example, perform the following steps:
- Open the Project tab on the right-hand side of the screen.
- Right-click on the
com.example
folder. - Go to New | Java Class and name the class
SampleFragment
.
As all custom activities extend the class Activity
, all custom fragments extend the class Fragment
.
Note
If you are using the support library, you can choose to either use the Fragment
class built into Android as of Honeycomb (API v11) or the android.support.v4.app.Fragment
class bundled as part of the support library. If you are planning on supporting devices pre-ICS (API v14), you must...