PreferenceHeaderFragmentCompat
public abstract class PreferenceHeaderFragmentCompat extends Fragment implements PreferenceFragmentCompat.OnPreferenceStartFragmentCallback
| java.lang.Object | ||
| ↳ | androidx.fragment.app.Fragment | |
| ↳ | androidx.preference.PreferenceHeaderFragmentCompat |
PreferenceHeaderFragmentCompat implements a two-pane fragment for preferences. The list pane is a container of preference headers. Tapping on a preference header swaps out the fragment shown in the detail pane. Subclasses are expected to implement onCreatePreferenceHeader to provide your own PreferenceFragmentCompat in the list pane. The preference header hierarchy is defined by either providing an XML resource or build in code through PreferenceFragmentCompat. In both cases, users need to use a PreferenceScreen as the root component in the hierarchy.
Usage:
class TwoPanePreference : PreferenceHeaderFragmentCompat() {
override fun onCreatePreferenceHeader(): PreferenceFragmentCompat {
return PreferenceHeader()
}
}
PreferenceHeaderFragmentCompat handles the fragment transaction when users defines a fragment or intent associated with the preference header. By default, the initial state fragment for the detail pane is set to the associated fragment that first found in preference headers. You can override onCreateInitialDetailFragment to provide the custom empty state fragment for the detail pane.
Summary
Public constructors |
|---|
Public methods |
|
|---|---|
final @NonNull SlidingPaneLayout |
Return the |
void |
Called when a fragment is first attached to its context. |
Fragment |
Override this method to set initial detail fragment that to be shown. |
abstract @NonNull PreferenceFragmentCompat |
Called to supply the preference header for this fragment. |
@NonNull View |
@CallSuperCalled to have the fragment instantiate its user interface view. |
boolean |
@CallSuperCalled when the user has clicked on a preference that has a fragment class name associated with it. |
void |
@CallSuperCalled immediately after |
void |
onViewStateRestored(Bundle savedInstanceState)Called when all saved state has been restored into the view hierarchy of the fragment. |
Inherited methods |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Public constructors
Public methods
getSlidingPaneLayout
public final @NonNull SlidingPaneLayout getSlidingPaneLayout()
Return the SlidingPaneLayout this fragment is currently controlling.
| Throws | |
|---|---|
IllegalStateException |
if the SlidingPaneLayout has not been created by |
onAttach
@CallSuper
public void onAttach(@NonNull Context context)
Called when a fragment is first attached to its context. onCreate will be called after this.
onCreateInitialDetailFragment
public Fragment onCreateInitialDetailFragment()
Override this method to set initial detail fragment that to be shown. The default implementation returns the first preference that has a fragment defined on it.
| Returns | |
|---|---|
Fragment |
Fragment The first fragment that found in the list of preference headers. |
onCreatePreferenceHeader
public abstract @NonNull PreferenceFragmentCompat onCreatePreferenceHeader()
Called to supply the preference header for this fragment. The subclasses are expected to call setPreferenceScreen(PreferenceScreen) either directly or via helper methods such as setPreferenceFromResource(int) to set headers.
onCreateView
@CallSuper
public @NonNull View onCreateView(
@NonNull 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. This will be called between onCreate and onViewCreated.
A default View can be returned by calling Fragment in your constructor. Otherwise, this method returns null.
It is recommended to only inflate the layout in this method and move logic that operates on the returned View to onViewCreated.
If you return a View from here, you will later be called in onDestroyView when the view is being released.
| Parameters | |
|---|---|
@NonNull LayoutInflater inflater |
The LayoutInflater object that can be used to inflate any views in the fragment, |
ViewGroup container |
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. |
Bundle savedInstanceState |
If non-null, this fragment is being re-constructed from a previous saved state as given here. |
onPreferenceStartFragment
@CallSuper
public boolean onPreferenceStartFragment(
@NonNull PreferenceFragmentCompat caller,
@NonNull Preference pref
)
Called when the user has clicked on a preference that has a fragment class name associated with it. The implementation should instantiate and switch to an instance of the given fragment.
| Parameters | |
|---|---|
@NonNull PreferenceFragmentCompat caller |
The fragment requesting navigation |
@NonNull Preference pref |
The preference requesting the fragment |
| Returns | |
|---|---|
boolean |
|
onViewCreated
@CallSuper
public void onViewCreated(@NonNull View view, Bundle savedInstanceState)
Called immediately after onCreateView 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 | |
|---|---|
@NonNull View view |
The View returned by |
Bundle savedInstanceState |
If non-null, this fragment is being re-constructed from a previous saved state as given here. |
onViewStateRestored
public void onViewStateRestored(Bundle savedInstanceState)
Called when all saved state has been restored into the view hierarchy of the fragment. This can be used to do initialization based on saved state that you are letting the view hierarchy track itself, such as whether check box widgets are currently checked. This is called after onViewCreated and before onStart.
| Parameters | |
|---|---|
Bundle savedInstanceState |
If the fragment is being re-created from a previous saved state, this is the state. |