Save the date! Android Dev Summit is coming to Mountain View, CA on November 7-8, 2018.
added in version 24.1.0
belongs to Maven artifact com.android.support:preference-leanback-v17:28.0.0-alpha1

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" />

    <Preference
        android:key="preference_with_icon"
        android:title="Preference with icon"
        android:summary="This preference has an icon"
        android:icon="@android:drawable/ic_menu_camera" />

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

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

        <SwitchPreference
            android:key="switch_preference"
            android:title="Switch preference"
            android:summary="This is a switch" />

        <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 constants

From interface android.content.ComponentCallbacks2

Public constructors

LeanbackSettingsFragment()

Public methods

View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
void onPause()
boolean onPreferenceDisplayDialog(PreferenceFragment caller, Preference pref)
abstract void onPreferenceStartInitialScreen()

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

void onResume()
void onViewCreated(View view, Bundle savedInstanceState)
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 24.1.0
LeanbackSettingsFragment ()

Public methods

onCreateView

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

Parameters
inflater LayoutInflater

container ViewGroup

savedInstanceState Bundle

Returns
View

onPause

void onPause ()

onPreferenceDisplayDialog

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

Parameters
caller PreferenceFragment

pref Preference

Returns
boolean

onPreferenceStartInitialScreen

added in version 24.1.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 ()

onViewCreated

void onViewCreated (View view, 
                Bundle savedInstanceState)

Parameters
view View

savedInstanceState Bundle

startImmersiveFragment

added in version 24.1.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 24.1.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.