LeanbackSettingsFragment

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


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"
        xmlns:app="http://schemas.android.com/apk/res-auto">

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

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

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

        <Preference
                android:key="icon"
                android:title="@string/title_icon_preference"
                android:summary="@string/summary_icon_preference"
                android:icon="@android:drawable/ic_menu_camera"/>

        <Preference
                android:key="single_line_title"
                android:title="@string/title_single_line_title_preference"
                android:summary="@string/summary_single_line_title_preference"
                app:singleLineTitle="true"/>
    </PreferenceCategory>

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

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

        <SwitchPreferenceCompat
                android:key="switch"
                android:title="@string/title_switch_preference"
                android:summary="@string/summary_switch_preference"/>

        <DropDownPreference
                android:key="dropdown"
                android:title="@string/title_dropdown_preference"
                android:entries="@array/entries"
                app:useSimpleSummaryProvider="true"
                android:entryValues="@array/entry_values"/>

        <SeekBarPreference
                android:key="seekbar"
                android:title="@string/title_seekbar_preference"
                android:max="10"
                android:defaultValue="5"/>
    </PreferenceCategory>

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

        <EditTextPreference
                android:key="edittext"
                android:title="@string/title_edittext_preference"
                app:useSimpleSummaryProvider="true"
                android:dialogTitle="@string/dialog_title_edittext_preference"/>

        <ListPreference
                android:key="list"
                android:title="@string/title_list_preference"
                app:useSimpleSummaryProvider="true"
                android:entries="@array/entries"
                android:entryValues="@array/entry_values"
                android:dialogTitle="@string/dialog_title_list_preference"/>

        <MultiSelectListPreference
                android:key="multi_select_list"
                android:title="@string/title_multi_list_preference"
                android:summary="@string/summary_multi_list_preference"
                android:entries="@array/entries"
                android:entryValues="@array/entry_values"
                android:dialogTitle="@string/dialog_title_multi_list_preference"/>
    </PreferenceCategory>

    <PreferenceCategory
            android:key="advanced"
            android:title="@string/advanced_attributes"
            app:initialExpandedChildrenCount="1">

        <Preference
                android:key="expandable"
                android:title="@string/title_expandable_preference"
                android:summary="@string/summary_expandable_preference"/>

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

        </Preference>

        <SwitchPreferenceCompat
                android:key="parent"
                android:title="@string/title_parent_preference"
                android:summary="@string/summary_parent_preference"/>

        <SwitchPreferenceCompat
                android:key="child"
                android:dependency="parent"
                android:title="@string/title_child_preference"
                android:summary="@string/summary_child_preference"/>

        <SwitchPreferenceCompat
                android:key="toggle_summary"
                android:title="@string/title_toggle_summary_preference"
                android:summaryOn="@string/summary_on_toggle_summary_preference"
                android:summaryOff="@string/summary_off_toggle_summary_preference"/>

        <Preference
                android:key="copyable"
                android:title="@string/title_copyable_preference"
                android:summary="@string/summary_copyable_preference"
                android:selectable="false"
                app:enableCopying="true"/>
    </PreferenceCategory>

</PreferenceScreen>

The sample implements onPreferenceStartFragment, onPreferenceStartScreen, and onPreferenceStartInitialScreen:

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

    @Override
    public boolean onPreferenceStartFragment(PreferenceFragmentCompat caller, Preference pref) {
        final Bundle args = pref.getExtras();
        final Fragment f = getChildFragmentManager().getFragmentFactory().instantiate(
                requireActivity().getClassLoader(), pref.getFragment());
        f.setArguments(args);
        f.setTargetFragment(caller, 0);
        if (f instanceof PreferenceFragmentCompat
                || f instanceof PreferenceDialogFragmentCompat) {
            startPreferenceFragment(f);
        } else {
            startImmersiveFragment(f);
        }
        return true;
    }

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

/**
 * The fragment that is embedded in SettingsFragment
 */
public static class DemoFragment extends LeanbackPreferenceFragmentCompat {

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

Summary

Public constructors

Public methods

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

This method is deprecated.

void

This method is deprecated.

boolean
abstract void

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

void

This method is deprecated.

void
onViewCreated(View view, Bundle savedInstanceState)

This method is deprecated.

void

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

void

Displays a preference fragment to the user.

Inherited Constants

From android.content.ComponentCallbacks2
static final int
static final int
static final int
static final int
static final int
static final int
static final int

Inherited methods

From android.app.Fragment
void
dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args)

This method is deprecated.

final boolean

This method is deprecated.

final Activity

This method is deprecated.

boolean

This method is deprecated.

boolean

This method is deprecated.

final Bundle

This method is deprecated.

final FragmentManager

This method is deprecated.

Context

This method is deprecated.

Transition

This method is deprecated.

Transition

This method is deprecated.

final FragmentManager

This method is deprecated.

final Object

This method is deprecated.

final int

This method is deprecated.

final LayoutInflater

This method is deprecated.

LoaderManager

This method is deprecated.

final Fragment

This method is deprecated.

Transition

This method is deprecated.

final Resources

This method is deprecated.

final boolean

This method is deprecated.

Transition

This method is deprecated.

Transition

This method is deprecated.

Transition

This method is deprecated.

final String
getString(int resId)

This method is deprecated.

final String

This method is deprecated.

final Fragment

This method is deprecated.

final int

This method is deprecated.

final CharSequence
getText(int resId)

This method is deprecated.

boolean

This method is deprecated.

View

This method is deprecated.

final int

This method is deprecated.

static Fragment
instantiate(Context context, String fname)

This method is deprecated.

final boolean

This method is deprecated.

final boolean

This method is deprecated.

final boolean

This method is deprecated.

final boolean

This method is deprecated.

final boolean

This method is deprecated.

final boolean

This method is deprecated.

final boolean

This method is deprecated.

final boolean

This method is deprecated.

void
onActivityCreated(Bundle savedInstanceState)

This method is deprecated.

void
onActivityResult(int requestCode, int resultCode, Intent data)

This method is deprecated.

void
onAttach(Context context)

This method is deprecated.

void
onAttachFragment(Fragment childFragment)

This method is deprecated.

void

This method is deprecated.

boolean

This method is deprecated.

void
onCreate(Bundle savedInstanceState)

This method is deprecated.

Animator
onCreateAnimator(int transit, boolean enter, int nextAnim)

This method is deprecated.

void
onCreateContextMenu(
    ContextMenu menu,
    View v,
    ContextMenu.ContextMenuInfo menuInfo
)

This method is deprecated.

void

This method is deprecated.

void

This method is deprecated.

void

This method is deprecated.

void

This method is deprecated.

void

This method is deprecated.

LayoutInflater
onGetLayoutInflater(Bundle savedInstanceState)

This method is deprecated.

void
onHiddenChanged(boolean hidden)

This method is deprecated.

void
onInflate(AttributeSet attrs, Bundle savedInstanceState)

This method is deprecated.

void

This method is deprecated.

void
onMultiWindowModeChanged(
    boolean isInMultiWindowMode,
    Configuration newConfig
)

This method is deprecated.

boolean

This method is deprecated.

void

This method is deprecated.

void
onPictureInPictureModeChanged(
    boolean isInPictureInPictureMode,
    Configuration newConfig
)

This method is deprecated.

void

This method is deprecated.

void
onRequestPermissionsResult(
    int requestCode,
    String[] permissions,
    int[] grantResults
)

This method is deprecated.

void

This method is deprecated.

void

This method is deprecated.

void

This method is deprecated.

void
onTrimMemory(int level)

This method is deprecated.

void
onViewStateRestored(Bundle savedInstanceState)

This method is deprecated.

void

This method is deprecated.

void

This method is deprecated.

final void
requestPermissions(String[] permissions, int requestCode)

This method is deprecated.

void

This method is deprecated.

void

This method is deprecated.

void

This method is deprecated.

void

This method is deprecated.

void

This method is deprecated.

void

This method is deprecated.

void

This method is deprecated.

void
setHasOptionsMenu(boolean hasMenu)

This method is deprecated.

void

This method is deprecated.

void
setMenuVisibility(boolean menuVisible)

This method is deprecated.

void

This method is deprecated.

void
setRetainInstance(boolean retain)

This method is deprecated.

void

This method is deprecated.

void

This method is deprecated.

void

This method is deprecated.

void
setTargetFragment(Fragment fragment, int requestCode)

This method is deprecated.

void
setUserVisibleHint(boolean isVisibleToUser)

This method is deprecated.

boolean

This method is deprecated.

void

This method is deprecated.

void
startActivityForResult(Intent intent, int requestCode)

This method is deprecated.

void
startIntentSenderForResult(
    IntentSender intent,
    int requestCode,
    Intent fillInIntent,
    int flagsMask,
    int flagsValues,
    int extraFlags,
    Bundle options
)

This method is deprecated.

void

This method is deprecated.

String

This method is deprecated.

void

This method is deprecated.

From androidx.preference.PreferenceFragment.OnPreferenceStartFragmentCallback
abstract boolean

Called when the user has clicked on a Preference that has a fragment class name associated with it.

From androidx.preference.PreferenceFragment.OnPreferenceStartScreenCallback
abstract boolean

Called when the user has clicked on a PreferenceScreen in order to navigate to a new screen of preferences.

Public constructors

LeanbackSettingsFragment

Added in 1.1.0
Deprecated in 1.1.0
public LeanbackSettingsFragment()

Public methods

onCreateView

Added in 1.1.0
Deprecated in 1.1.0
public View onCreateView(
    LayoutInflater inflater,
    ViewGroup container,
    Bundle savedInstanceState
)

onPause

Added in 1.1.0
Deprecated in 1.1.0
public void onPause()

onPreferenceDisplayDialog

Added in 1.1.0
Deprecated in 1.1.0
public boolean onPreferenceDisplayDialog(
    @NonNull PreferenceFragment caller,
    Preference pref
)
Parameters
@NonNull PreferenceFragment caller

The fragment containing the preference requesting the dialog

Preference pref

The preference requesting the dialog

Returns
boolean

true if the dialog creation has been handled

onPreferenceStartInitialScreen

Added in 1.1.0
Deprecated in 1.1.0
public abstract void onPreferenceStartInitialScreen()

Called to instantiate the initial androidx.preference.PreferenceFragment to be shown in this fragment. Implementations are expected to call startPreferenceFragment.

onResume

Added in 1.1.0
Deprecated in 1.1.0
public void onResume()

onViewCreated

Added in 1.1.0
Deprecated in 1.1.0
public void onViewCreated(View view, Bundle savedInstanceState)

startImmersiveFragment

Added in 1.1.0
Deprecated in 1.1.0
public void startImmersiveFragment(@NonNull Fragment fragment)

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

Parameters
@NonNull Fragment fragment

Fragment instance to be added.

startPreferenceFragment

Added in 1.1.0
Deprecated in 1.1.0
public void startPreferenceFragment(@NonNull 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
@NonNull Fragment fragment

Fragment instance to be added.