AbstractListDetailFragment
public abstract class AbstractListDetailFragment extends Fragment
| java.lang.Object | ||
| ↳ | androidx.fragment.app.Fragment | |
| ↳ | androidx.navigation.fragment.AbstractListDetailFragment |
A fragment supports adaptive two-pane layout. The first child is a list pane, which could be a content list or browser, and the second child is NavHostFragment which controls to navigate between different detail views.
Implementation of the fragment should override this class and implement AbstractListDetailFragment.onCreateListPaneView to supply custom view for the list pane. The fragment provides default NavHostFragment with a NavGraph ID passed in the fragment, and it can be overridden by AbstractListDetailFragment.onCreateDetailPaneNavHostFragment and provide custom NavHostFragment.
Summary
Public constructors |
|---|
Public methods |
|
|---|---|
final @NonNull NavHostFragment |
Return the |
final @NonNull SlidingPaneLayout |
Return the |
@NonNull NavHostFragment |
Return an alternative |
abstract @NonNull View |
onCreateListPaneView(Provide a list pane view for the fragment. |
final @NonNull View |
@CallSuperCreate the view for the fragment. |
void |
@CallSuperCalled when a fragment is being created as part of a view layout inflation, typically from setting the content view of an activity. |
void |
onListPaneViewCreated(@NonNull View view, Bundle savedInstanceState)Provides list pane view created in the fragment. |
void |
@CallSuperCalled to ask the fragment to save its current dynamic state, so it can later be reconstructed in a new instance if its process is restarted. |
final void |
@CallSuperThis method provides a callback |
void |
@CallSuperCalled when all saved state has been restored into the view hierarchy of the fragment. |
Inherited methods |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Public constructors
Public methods
getDetailPaneNavHostFragment
public final @NonNull NavHostFragment getDetailPaneNavHostFragment()
Return the NavHostFragment this fragment uses
| Throws | |
|---|---|
kotlin.IllegalStateException |
if the NavHostFragment has not been created by {@link #onCreateView}. |
getSlidingPaneLayout
public final @NonNull SlidingPaneLayout getSlidingPaneLayout()
Return the SlidingPaneLayout this fragment is currently controlling.
| Throws | |
|---|---|
kotlin.IllegalStateException |
if the SlidingPaneLayout has not been created by |
onCreateDetailPaneNavHostFragment
public @NonNull NavHostFragment onCreateDetailPaneNavHostFragment()
Return an alternative NavHostFragment to swap the default NavHostFragment in the fragment. This method get called when creating the view of the fragment.
onCreateListPaneView
public abstract @NonNull View onCreateListPaneView(
@NonNull LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState
)
Provide a list pane view for the fragment. Called when creating the view of the fragment.
| Parameters | |
|---|---|
@NonNull LayoutInflater inflater |
The |
ViewGroup container |
The parent view of the list pane view. The parent view can be used to generate the LayoutParams of the view. |
Bundle savedInstanceState |
The previous saved state of the fragment. |
onCreateView
@CallSuper
public final @NonNull View onCreateView(
@NonNull LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState
)
Create the view for the fragment. This method provides two callbacks to instantiate a list pane view and a NavHostFragment to control navigation between different detail views.
| Parameters | |
|---|---|
@NonNull LayoutInflater inflater |
The |
ViewGroup container |
The parent view that the fragment's UI should be attached to. |
Bundle savedInstanceState |
The previous saved state of the fragment. |
onInflate
@CallSuper
public void onInflate(
@NonNull Context context,
@NonNull AttributeSet attrs,
Bundle savedInstanceState
)
Called when a fragment is being created as part of a view layout inflation, typically from setting the content view of an activity. This may be called immediately after the fragment is created from a FragmentContainerView in a layout file. Note this is before the fragment's onAttach has been called; all you should do here is parse the attributes and save them away.
This is called the first time the fragment is inflated. If it is being inflated into a new instance with saved state, this method will not be called a second time for the restored state fragment.
Here is a typical implementation of a fragment that can take parameters both through attributes supplied here as well from getArguments:
public static class MyFragment extends Fragment { CharSequence mLabel; /** * Create a new instance of MyFragment that will be initialized * with the given arguments. */ static MyFragment newInstance(CharSequence label) { MyFragment f = new MyFragment(); Bundle b = new Bundle(); b.putCharSequence("label", label); f.setArguments(b); return f; } /** * Parse attributes during inflation from a view hierarchy into the * arguments we handle. */ @Override public void onInflate(@NonNull Context context, @NonNull AttributeSet attrs, @Nullable Bundle savedInstanceState) { super.onInflate(context, attrs, savedInstanceState); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FragmentArguments); mLabel = a.getText(R.styleable.FragmentArguments_android_label); a.recycle(); } /** * During creation, if arguments have been supplied to the fragment * then parse those out. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Bundle args = getArguments(); if (args != null) { CharSequence label = args.getCharSequence("label"); if (label != null) { mLabel = label; } } } /** * Create the view for this fragment, using the arguments given to it. */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.hello_world, container, false); View tv = v.findViewById(R.id.text); ((TextView)tv).setText(mLabel != null ? mLabel : "(no label)"); ViewCompat.setBackground( tv, ContextCompat.getDrawable(getContext(), android.R.drawable.gallery_thumb)); return v; } }
Note that parsing the XML attributes uses a "styleable" resource. The declaration for the styleable used here is:
<declare-styleable name="FragmentArguments"> <attr name="android:label" /> </declare-styleable>
The fragment can then be declared within its activity's content layout through a tag like this:
<androidx.fragment.app.FragmentContainerView class="com.example.android.supportv4.app.FragmentArgumentsSupport$MyFragment" android:id="@+id/embedded" android:layout_width="0px" android:layout_height="wrap_content" android:layout_weight="1" android:label="@string/fragment_arguments_embedded" />
This fragment can also be created dynamically from arguments given at runtime in the arguments Bundle; here is an example of doing so at creation of the containing activity:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_arguments_support); if (savedInstanceState == null) { // First-time init; create fragment to embed in activity. FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); Fragment newFragment = MyFragment.newInstance("From Arguments"); ft.add(R.id.created, newFragment); ft.commit(); } }
| Parameters | |
|---|---|
@NonNull Context context |
The Activity that is inflating this fragment. |
@NonNull AttributeSet attrs |
The attributes at the tag where the fragment is being created. |
Bundle savedInstanceState |
If the fragment is being re-created from a previous saved state, this is the state. |
onListPaneViewCreated
public void onListPaneViewCreated(@NonNull View view, Bundle savedInstanceState)
Provides list pane view created in the fragment. Called when the fragment's onViewCreated get called.
| Parameters | |
|---|---|
@NonNull View view |
The list pane view created by |
Bundle savedInstanceState |
The previous saved state of the fragment. |
onSaveInstanceState
@CallSuper
public void onSaveInstanceState(@NonNull Bundle outState)
Called to ask the fragment to save its current dynamic state, so it can later be reconstructed in a new instance if its process is restarted. If a new instance of the fragment later needs to be created, the data you place in the Bundle here will be available in the Bundle given to onCreate, onCreateView, and onViewCreated.
This corresponds to Activity.onSaveInstanceState(Bundle) and most of the discussion there applies here as well. Note however: this method may be called at any time before onDestroy. There are many situations where a fragment may be mostly torn down (such as when placed on the back stack with no UI showing), but its state will not be saved until its owning activity actually needs to save its state.
onViewCreated
@CallSuper
public final void onViewCreated(@NonNull View view, Bundle savedInstanceState)
This method provides a callback onListPaneViewCreated after the view hierarchy has been completely created.
| Parameters | |
|---|---|
@NonNull View view |
The view returned by |
Bundle savedInstanceState |
The previous saved state of the fragment. |
| See also | |
|---|---|
onListPaneViewCreated |
onViewStateRestored
@CallSuper
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. |