Skip to content

Most visited

Recently visited

navigation

LeanbackSettingsFragment

public abstract class LeanbackSettingsFragment
extends Fragment implements PreferenceFragment.OnPreferenceStartFragmentCallback, PreferenceFragment.OnPreferenceStartScreenCallback, PreferenceFragment.OnPreferenceDisplayDialogCallback

java.lang.Object
   ↳ android.app.Fragment
     ↳ android.support.v17.preference.LeanbackSettingsFragment


This fragment provides a container for displaying a LeanbackPreferenceFragment

The following sample code shows a simple leanback preference fragment that is populated from a resource. The resource it loads is:

<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:title="@string/root_title">

    <Preference
        android:key="basic_preference"
        android:title="@string/title_basic_preference"
        android:summary="@string/summary_basic_preference" />

    <Preference
        android:key="stylish_preference"
        android:title="@string/title_stylish_preference"
        android:summary="@string/summary_stylish_preference" />

    <PreferenceCategory
        android:title="@string/inline_preferences">

        <CheckBoxPreference
            android:key="checkbox_preference"
            android:title="@string/title_checkbox_preference"
            android:summary="@string/summary_checkbox_preference" />

        <DropDownPreference
            android:key="dropdown_preference"
            android:title="@string/title_dropdown_preference"
            android:summary="@string/summary_dropdown_preference"
            android:entries="@array/entries_list_preference"
            android:entryValues="@array/entryvalues_list_preference" />

    </PreferenceCategory>

    <PreferenceCategory
        android:title="@string/dialog_based_preferences">

        <EditTextPreference
            android:key="edittext_preference"
            android:title="@string/title_edittext_preference"
            android:summary="@string/summary_edittext_preference"
            android:dialogTitle="@string/dialog_title_edittext_preference" />

        <ListPreference
            android:key="list_preference"
            android:title="@string/title_list_preference"
            android:summary="@string/summary_list_preference"
            android:entries="@array/entries_list_preference"
            android:entryValues="@array/entryvalues_list_preference"
            android:dialogTitle="@string/dialog_title_list_preference" />

        <MultiSelectListPreference
            android:key="multi_select_list_preference"
            android:title="@string/title_multi_list_preference"
            android:summary="@string/summary_multi_list_preference"
            android:entries="@array/entries_list_preference"
            android:entryValues="@array/entryvalues_list_preference"
            android:dialogTitle="@string/dialog_title_multi_list_preference" />

    </PreferenceCategory>

    <PreferenceCategory
        android:title="@string/launch_preferences">

        <!-- This PreferenceScreen tag serves as a screen break (similar to page break
             in word processing). Like for other preference types, we assign a key
             here so it is able to save and restore its instance state. -->
        <PreferenceScreen
            android:key="screen_preference"
            android:title="@string/title_screen_preference"
            android:summary="@string/summary_screen_preference">

            <!-- You can place more preferences here that will be shown on the next screen. -->

            <CheckBoxPreference
                android:key="next_screen_checkbox_preference"
                android:title="@string/title_next_screen_toggle_preference"
                android:summary="@string/summary_next_screen_toggle_preference" />

        </PreferenceScreen>

        <PreferenceScreen
            android:title="@string/title_intent_preference"
            android:summary="@string/summary_intent_preference">

            <intent android:action="android.intent.action.VIEW"
                android:data="http://www.android.com" />

        </PreferenceScreen>

    </PreferenceCategory>

    <PreferenceCategory
        android:title="@string/preference_attributes">

        <CheckBoxPreference
            android:key="parent_checkbox_preference"
            android:title="@string/title_parent_preference"
            android:summary="@string/summary_parent_preference" />

        <!-- The visual style of a child is defined by this styled theme attribute. -->
        <CheckBoxPreference
            android:key="child_checkbox_preference"
            android:dependency="parent_checkbox_preference"
            android:layout="?android:attr/preferenceLayoutChild"
            android:title="@string/title_child_preference"
            android:summary="@string/summary_child_preference" />

    </PreferenceCategory>

</PreferenceScreen>

The sample implements onPreferenceStartFragment(PreferenceFragment, Preference), onPreferenceStartScreen(PreferenceFragment, PreferenceScreen), and onPreferenceStartInitialScreen():

public static class SettingsFragment extends LeanbackSettingsFragment {
    @Override
    public void onPreferenceStartInitialScreen() {
        startPreferenceFragment(new PrefsFragment());
    }

    @Override
    public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
        final Fragment f =
                Fragment.instantiate(getActivity(), pref.getFragment(), pref.getExtras());
        f.setTargetFragment(caller, 0);
        if (f instanceof PreferenceFragment || f instanceof PreferenceDialogFragment) {
            startPreferenceFragment(f);
        } else {
            startImmersiveFragment(f);
        }
        return true;
    }

    @Override
    public boolean onPreferenceStartScreen(PreferenceFragment caller, PreferenceScreen pref) {
        final Fragment f = new PrefsFragment();
        final Bundle args = new Bundle(1);
        args.putString(PreferenceFragment.ARG_PREFERENCE_ROOT, pref.getKey());
        f.setArguments(args);
        startPreferenceFragment(f);
        return true;
    }
}

public static class PrefsFragment extends LeanbackPreferenceFragment {

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        // Load the preferences from an XML resource
        setPreferencesFromResource(R.xml.preferences, rootKey);
    }
}

Summary

Inherited XML attributes

From class android.app.Fragment

Inherited constants

From interface android.content.ComponentCallbacks2

Public constructors

LeanbackSettingsFragment()

Public methods

View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

Called to have the fragment instantiate its user interface view.

void onPause()

Called when the Fragment is no longer resumed.

boolean onPreferenceDisplayDialog(PreferenceFragment caller, Preference pref)
abstract void onPreferenceStartInitialScreen()

Called to instantiate the initial PreferenceFragment to be shown in this fragment.

void onResume()

Called when the fragment is visible to the user and actively running.

void onViewCreated(View view, Bundle savedInstanceState)

Called immediately after onCreateView(LayoutInflater, ViewGroup, Bundle) has returned, but before any saved state has been restored in to the view.

void startImmersiveFragment(Fragment fragment)

Displays a fragment to the user, temporarily replacing the contents of this fragment.

void startPreferenceFragment(Fragment fragment)

Displays a preference fragment to the user.

Inherited methods

From class android.app.Fragment
From class java.lang.Object
From interface android.content.ComponentCallbacks2
From interface android.view.View.OnCreateContextMenuListener
From interface android.support.v14.preference.PreferenceFragment.OnPreferenceStartFragmentCallback
From interface android.support.v14.preference.PreferenceFragment.OnPreferenceStartScreenCallback
From interface android.support.v14.preference.PreferenceFragment.OnPreferenceDisplayDialogCallback
From interface android.content.ComponentCallbacks

Public constructors

LeanbackSettingsFragment

added in version 23.0.0
LeanbackSettingsFragment ()

Public methods

onCreateView

View onCreateView (LayoutInflater inflater, 
                ViewGroup container, 
                Bundle savedInstanceState)

Called to have the fragment instantiate its user interface view. This is optional, and non-graphical fragments can return null (which is the default implementation). This will be called between onCreate(Bundle) and onActivityCreated(Bundle).

If you return a View from here, you will later be called in onDestroyView() when the view is being released.

Parameters
inflater LayoutInflater: The LayoutInflater object that can be used to inflate any views in the fragment,

container ViewGroup: If non-null, this is the parent view that the fragment's UI should be attached to. The fragment should not add the view itself, but this can be used to generate the LayoutParams of the view.

savedInstanceState Bundle: If non-null, this fragment is being re-constructed from a previous saved state as given here.

Returns
View Return the View for the fragment's UI, or null.

onPause

void onPause ()

Called when the Fragment is no longer resumed. This is generally tied to Activity.onPause of the containing Activity's lifecycle.

onPreferenceDisplayDialog

added in version 23.0.0
boolean onPreferenceDisplayDialog (PreferenceFragment caller, 
                Preference pref)

Parameters
caller PreferenceFragment

pref Preference

Returns
boolean

onPreferenceStartInitialScreen

added in version 23.0.0
void onPreferenceStartInitialScreen ()

Called to instantiate the initial PreferenceFragment to be shown in this fragment. Implementations are expected to call startPreferenceFragment(android.app.Fragment).

onResume

void onResume ()

Called when the fragment is visible to the user and actively running. This is generally tied to Activity.onResume of the containing Activity's lifecycle.

onViewCreated

void onViewCreated (View view, 
                Bundle savedInstanceState)

Called immediately after onCreateView(LayoutInflater, ViewGroup, Bundle) has returned, but before any saved state has been restored in to the view. This gives subclasses a chance to initialize themselves once they know their view hierarchy has been completely created. The fragment's view hierarchy is not however attached to its parent at this point.

Parameters
view View: The View returned by onCreateView(LayoutInflater, ViewGroup, Bundle).

savedInstanceState Bundle: If non-null, this fragment is being re-constructed from a previous saved state as given here.

startImmersiveFragment

added in version 23.0.0
void startImmersiveFragment (Fragment fragment)

Displays a fragment to the user, temporarily replacing the contents of this fragment.

Parameters
fragment Fragment: Fragment instance to be added.

startPreferenceFragment

added in version 23.0.0
void startPreferenceFragment (Fragment fragment)

Displays a preference fragment to the user. This method can also be used to display list-style fragments on top of the stack of preference fragments.

Parameters
fragment Fragment: Fragment instance to be added.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)