6 Fragments

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Fragments

A Fragment represents a behavior or a portion of user interface in a FragmentActivity.


You can combine multiple fragments in a single activity to build a multi-pane UI and reuse
a fragment in multiple activities. You can think of a fragment as a modular section of an
activity, which has its own lifecycle, receives its own input events, and which you can add
or remove while the activity is running (sort of like a "sub activity" that you can reuse in
different activities).

A fragment must always be hosted in an activity and the fragment's lifecycle is directly
affected by the host activity's lifecycle. For example, when the activity is paused, so are all
fragments in it, and when the activity is destroyed, so are all fragments. However, while an
activity is running (it is in the resumed lifecycle state), you can manipulate each fragment
independently, such as add or remove them. When you perform such a fragment
transaction, you can also add it to a back stack that's managed by the activity—each back
stack entry in the activity is a record of the fragment transaction that occurred. The back
stack allows the user to reverse a fragment transaction (navigate backwards), by pressing
the Back button.

You can insert a fragment into your activity layout by declaring the fragment in the
activity's layout file, as a <fragment> element, or from your application code by adding it
to an existing ViewGroup(like FrameLayout).
Creating a Fragment

To create a fragment, you must create a


subclass of Fragment (or an existing
subclass of it). The Fragment class has
code that looks a lot like an Activity. It
contains callback methods similar to an
activity, such
as onCreate(), onStart(), onPause(),
and onStop(). In fact, if you're
converting an existing Android
application to use fragments, you might
simply move code from your activity's
callback methods into the respective
callback methods of your fragment.

There are also a few subclasses that you


might want to extend, instead of the
base Fragment class:

DialogFragment

Displays a floating dialog. Using


this class to create a dialog is a
good alternative to using the
dialog helper methods in
the Activity class, because you
can incorporate a fragment dialog
into the back stack of fragments
managed by the activity, allowing
the user to return to a dismissed
fragment.

ListFragment

Displays a list of items that are


managed by an adapter (such as
a SimpleCursorAdapter), similar
to ListActivity. It provides several methods for managing a list view, such
as the onListItemClick() callback to handle click events. (Note that the
preferred method for displaying a list is to use RecyclerView instead of
ListView. In this case you would need to create a fragment that includes
a RecyclerView in its layout. See Create a List with RecyclerView to learn
how.)

PreferenceFragmentCompat

Displays a hierarchy of Preference objects as a list. This is used to create a


settings screen for your application.

Adding a fragment to an activity


Usually, a fragment contributes a portion of UI to the host activity, which is
embedded as a part of the activity's overall view hierarchy. There are two ways you
can add a fragment to the activity layout:

• Declare the fragment inside the activity's layout file.

In this case, you can specify layout properties for the fragment as if it were a view.
For example, here's the layout file for an activity with two fragments:

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.example.news.ArticleListFragment"
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.example.news.ArticleReaderFragment"
android:id="@+id/viewer"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
The android:name attribute in the <fragment> specifies the Fragment class to
instantiate in the layout.

When the system creates this activity layout, it instantiates each fragment specified
in the layout and calls the onCreateView() method for each one, to retrieve each
fragment's layout. The system inserts the View returned by the fragment directly
in place of the <fragment> element.

• Programmatically add the fragment to an existing ViewGroup.

At any time while your activity is running, you can add fragments to your activity
layout. You simply need to specify a ViewGroup in which to place the fragment.

To make fragment transactions in your activity (such as add, remove, or replace a


fragment), you must use APIs from FragmentTransaction. You can get an instance
of FragmentTransaction from your FragmentActivity like this:

FragmentManager fragmentManager = getSupportFragmentManager();


FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();

You can then add a fragment using the add() method, specifying the fragment to
add and the view in which to insert it. For example:

ExampleFragment fragment = new ExampleFragment();


fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();

The first argument passed to add() is the ViewGroup in which the fragment should
be placed, specified by resource ID, and the second parameter is the fragment to
add.

Once you've made your changes with FragmentTransaction, you must


call commit() for the changes to take effect.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy