Creating A Fragment - Android Developers PDF
Creating A Fragment - Android Developers PDF
Creating A Fragment - Android Developers PDF
Training
API Guides
Design
Reference
Getting Started
Develop
Tools
Console
Distribute
Google Services
Samples
Preview
Creating a Fragment
Building a Flexible UI
Communicating with Other Fragments
Saving Data
Interacting with Other Apps
Before you begin this lesson, you must set up your Android
Previous
project to use the Support Library. If you have not used the
Support Library before, set up your project to use the v4 library
Try it out
Next
To create a fragment, extend the Fragment class, then override key lifecycle methods to insert your app logic,
similar to the way you would with an Activity class.
One difference when creating a Fragment is that you must use the onCreateView() callback to define the
layout. In fact, this is the only callback you need in order to get a fragment running. For example, here's a simple
fragment that specifies its own layout:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.ViewGroup;
public class ArticleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.article_view, container, false);
}
}
TV
Just like an activity, a fragment should implement other lifecycle callbacks that allow you to manage its state as
Building Apps for
Auto
it is added or removed from the activity and as the activity transitions between its lifecycle states. For instance,
when the activity's onPause() method is called, any fragments in the activity also receive a call to
onPause().
More information about the fragment lifecycle and callback methods is available in the Fragments developer
guide.
While fragments are reusable, modular UI components, each instance of a Fragment class must be associated
Best Practices for
User Input
Best Practices for
Background Jobs
Best Practices for
Performance
Best Practices for
Security & Privacy
Best Practices for
Permissions & Identifiers
Best Practices for
Testing
Using Google Play to
Distribute & Monetize
with a parent FragmentActivity. You can achieve this association by defining each fragment within your
activity layout XML file.
Note: FragmentActivity is a special activity provided in the Support Library to handle fragments on
system versions older than API level 11. If the lowest system version you support is API level 11 or higher,
then you can use a regular Activity.
Here is an example layout file that adds two fragments to an activity when the device screen is considered
"large" (specified by the large qualifier in the directory name).
res/layout-large/news_articles.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment android:name="com.example.android.fragments.HeadlinesFragment"
android:id="@+id/headlines_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.example.android.fragments.ArticleFragment"
android:id="@+id/article_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
Tip: For more about creating layouts for different screen sizes, read Supporting Different Screen Sizes.
If you're using the v7 appcompat library, your activity should instead extend AppCompatActivity, which is a
subclass of FragmentActivity. For more information, read Adding the App Bar).
Note: When you add a fragment to an activity layout by defining the fragment in the layout XML file, you
cannot remove the fragment at runtime. If you plan to swap your fragments in and out during user interaction,
you must add the fragment to the activity when the activity first starts, as shown in the next lesson.
Blog Support
Except as noted, this content is licensed under Creative Commons Attribution 2.5. For details and restrictions, see the Content
License.
About Android | Auto | TV | Wear | Legal
English