{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

ListFragment

{% setvar page_path %}androidx/fragment/app/ListFragment.html{% endsetvar %} {% setvar can_switch %}1{% endsetvar %} {% include "reference/_java_switcher2.md" %}

public class ListFragment extends Fragment

Object
   ↳ Fragment
     ↳ ListFragment

Static library support version of the framework's android.app.ListFragment. Used to write apps that run on platforms prior to Android 3.0. When running on Android 3.0 or above, this implementation is still used; it does not try to switch to the framework's implementation. See the framework SDK documentation for a class overview.

Summary

Public constructors

Public methods

@Nullable ListAdapter

Get the ListAdapter associated with this fragment's ListView.

@NonNull ListView

Get the fragment's list view widget.

@NonNull long

Get the cursor row ID of the currently selected list item.

@NonNull int

Get the position of the currently selected list item.

@Nullable View
onCreateView(
    @NonNull LayoutInflater inflater,
    @Nullable ViewGroup container,
    @Nullable Bundle savedInstanceState
)

Provide default implementation to return a simple list view.

@NonNull void

Detach from list view.

@NonNull void
onListItemClick(
    @NonNull ListView l,
    @NonNull View v,
    @NonNull int position,
    @NonNull long id
)

This method will be called when an item in the list is selected.

@NonNull void
onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)

Attach to list view once the view hierarchy has been created.

final @NonNull ListAdapter

Get the ListAdapter associated with this fragment's ListView.

@NonNull void

The default content for a ListFragment has a TextView that can be shown when the list is empty.

@NonNull void
setListAdapter(@Nullable ListAdapter adapter)

Provide the cursor for the list view.

@NonNull void
setListShown(@NonNull boolean shown)

Control whether the list is being displayed.

@NonNull void

Like setListShown, but no animation is used when transitioning from the previous state.

@NonNull void
setSelection(@NonNull int position)

Set the currently selected list item to the specified position with the adapter's data

Inherited methods

From class Fragment
@NonNull void
dump(
    @NonNull String prefix,
    @Nullable FileDescriptor fd,
    @NonNull PrintWriter writer,
    @Nullable Array<@NonNull String> args
)

Print the Fragments's state into the given stream.

final @NonNull boolean

Subclasses can not override equals().

final @Nullable FragmentActivity

Return the FragmentActivity this fragment is currently associated with.

@NonNull boolean

Returns whether the the exit transition and enter transition overlap or not.

@NonNull boolean

Returns whether the the return transition and reenter transition overlap or not.

final @Nullable Bundle

Return the arguments supplied when the fragment was instantiated, if any.

final @NonNull FragmentManager

Return a private FragmentManager for placing and managing Fragments inside of this Fragment.

@Nullable Context

Return the Context this fragment is currently associated with.

@NonNull Factory

The Fragment's arguments when this is first called will be used as the defaults to any androidx.lifecycle.SavedStateHandle passed to a view model created using this factory.

@Nullable Object

Returns the Transition that will be used to move Views into the initial scene.

@Nullable Object

Returns the Transition that will be used to move Views out of the scene when the fragment is removed, hidden, or detached when not popping the back stack.

final @Nullable FragmentManager

This method is deprecated.

This has been removed in favor of getParentFragmentManager() which throws an IllegalStateException if the FragmentManager is null.

final @Nullable Object

Return the host object of this fragment.

final @NonNull int

Return the identifier this fragment is known by.

final @NonNull LayoutInflater

Returns the cached LayoutInflater used to inflate Views of this Fragment.

@NonNull Lifecycle

Overriding this method is no longer supported and this method will be made final in a future version of Fragment.

@NonNull LoaderManager

This method is deprecated.

Use LoaderManager.getInstance(this).

final @Nullable Fragment

Returns the parent Fragment containing this Fragment.

final @NonNull FragmentManager

Return the FragmentManager for interacting with fragments associated with this fragment's activity.

@Nullable Object

Returns the Transition that will be used to move Views in to the scene when returning due to popping a back stack.

final @NonNull Resources

Return requireActivity().getResources().

final @NonNull boolean

This method is deprecated.

Instead of retaining the Fragment itself, use a non-retained Fragment and keep retained state in a ViewModel attached to that Fragment.

@Nullable Object

Returns the Transition that will be used to move Views out of the scene when the Fragment is preparing to be removed, hidden, or detached because of popping the back stack.

final @NonNull SavedStateRegistry
@Nullable Object

Returns the Transition that will be used for shared elements transferred into the content Scene.

@Nullable Object

Return the Transition that will be used for shared elements transferred back during a pop of the back stack.

final @NonNull String
getString(@NonNull int resId)

Return a localized string from the application's package's default string table.

final @NonNull String
getString(@NonNull int resId, @Nullable Array<@NonNull Object> formatArgs)

Return a localized formatted string from the application's package's default string table, substituting the format arguments as defined in java.util.Formatter and format.

final @Nullable String

Get the tag name of the fragment, if specified.

final @Nullable Fragment

This method is deprecated.

Instead of using a target fragment to pass results, use FragmentManager#setFragmentResult(String, Bundle) to deliver results to FragmentResultListener instances registered by other fragments via FragmentManager#setFragmentResultListener(String, LifecycleOwner, FragmentResultListener).

final @NonNull int

This method is deprecated.

When using the target fragment replacement of FragmentManager#setFragmentResultListener(String, LifecycleOwner, FragmentResultListener) and FragmentManager#setFragmentResult(String, Bundle), consider using setArguments to pass a {@code requestKey} if you need to support dynamic request keys.

final @NonNull CharSequence
getText(@NonNull int resId)

Return a localized, styled CharSequence from the application's package's default string table.

@NonNull boolean

This method is deprecated.

Use FragmentTransaction#setMaxLifecycle(Fragment, Lifecycle.State) instead.

@Nullable View

Get the root view for the fragment's layout (the one returned by onCreateView), if provided.

@NonNull LifecycleOwner

Get a LifecycleOwner that represents the Fragment's View lifecycle.

@NonNull LiveData<LifecycleOwner>

Retrieve a LiveData which allows you to observe the lifecycle of the Fragment's View.

@NonNull ViewModelStore

Returns the ViewModelStore associated with this Fragment

final @NonNull int

Subclasses can not override hashCode().

static @NonNull Fragment
instantiate(@NonNull Context context, @NonNull String fname)

This method is deprecated.

Use FragmentManager#getFragmentFactory() and FragmentFactory#instantiate(ClassLoader, String)

static @NonNull Fragment
instantiate(
    @NonNull Context context,
    @NonNull String fname,
    @Nullable Bundle args
)

This method is deprecated.

Use FragmentManager#getFragmentFactory() and FragmentFactory#instantiate(ClassLoader, String), manually calling setArguments on the returned Fragment.

final @NonNull boolean

Return true if the fragment is currently added to its activity.

final @NonNull boolean

Return true if the fragment has been explicitly detached from the UI.

final @NonNull boolean

Return true if the fragment has been hidden.

final @NonNull boolean

Return true if the layout is included as part of an activity view hierarchy via the <fragment> tag.

final @NonNull boolean

Return true if this fragment is currently being removed from its activity.

final @NonNull boolean

Return true if the fragment is in the resumed state.

final @NonNull boolean

Returns true if this fragment is added and its state has already been saved by its host.

final @NonNull boolean

Return true if the fragment is currently visible to the user.

@NonNull void
onActivityCreated(@Nullable Bundle savedInstanceState)

This method is deprecated.

use onViewCreated for code touching the Fragment's view and onCreate for other initialization.

@NonNull void
onActivityResult(
    @NonNull int requestCode,
    @NonNull int resultCode,
    @Nullable Intent data
)

This method is deprecated.

use registerForActivityResult with the appropriate ActivityResultContract and handling the result in the callback.

@NonNull void
onAttach(@NonNull Context context)

Called when a fragment is first attached to its context.

@NonNull void
onAttach(@NonNull Activity activity)

This method is deprecated.

See onAttach.

@NonNull void

This method is deprecated.

The responsibility for listening for fragments being attached has been moved to FragmentManager.

@NonNull void
onConfigurationChanged(@NonNull Configuration newConfig)
@NonNull boolean
onContextItemSelected(@NonNull MenuItem item)

This hook is called whenever an item in a context menu is selected.

@NonNull void
onCreate(@Nullable Bundle savedInstanceState)

Called to do initial creation of a fragment.

@Nullable Animation
onCreateAnimation(
    @NonNull int transit,
    @NonNull boolean enter,
    @NonNull int nextAnim
)

Called when a fragment loads an animation.

@Nullable Animator
onCreateAnimator(
    @NonNull int transit,
    @NonNull boolean enter,
    @NonNull int nextAnim
)

Called when a fragment loads an animator.

@NonNull void
onCreateContextMenu(
    @NonNull ContextMenu menu,
    @NonNull View v,
    @Nullable ContextMenuInfo menuInfo
)

Called when a context menu for the {@code view} is about to be shown.

@NonNull void
onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater)

Initialize the contents of the Fragment host's standard options menu.

@NonNull void

Called when the fragment is no longer in use.

@NonNull void

Called when this fragment's option menu items are no longer being included in the overall options menu.

@NonNull void

Called when the fragment is no longer attached to its activity.

@NonNull LayoutInflater
onGetLayoutInflater(@Nullable Bundle savedInstanceState)

Returns the LayoutInflater used to inflate Views of this Fragment.

@NonNull void
onHiddenChanged(@NonNull boolean hidden)

Called when the hidden state (as returned by isHidden of the fragment has changed.

@NonNull void
onInflate(
    @NonNull Context context,
    @NonNull AttributeSet attrs,
    @Nullable 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.

@NonNull void
onInflate(
    @NonNull Activity activity,
    @NonNull AttributeSet attrs,
    @Nullable Bundle savedInstanceState
)

This method is deprecated.

See onInflate.

@NonNull void
@NonNull void
onMultiWindowModeChanged(@NonNull boolean isInMultiWindowMode)

Called when the Fragment's activity changes from fullscreen mode to multi-window mode and visa-versa.

@NonNull boolean
onOptionsItemSelected(@NonNull MenuItem item)

This hook is called whenever an item in your options menu is selected.

@NonNull void

This hook is called whenever the options menu is being closed (either by the user canceling the menu with the back/menu button, or when an item is selected).

@NonNull void

Called when the Fragment is no longer resumed.

@NonNull void
onPictureInPictureModeChanged(@NonNull boolean isInPictureInPictureMode)

Called by the system when the activity changes to and from picture-in-picture mode.

@NonNull void

Prepare the Fragment host's standard options menu to be displayed.

@NonNull void
onPrimaryNavigationFragmentChanged(
    @NonNull boolean isPrimaryNavigationFragment
)

Callback for when the primary navigation state of this Fragment has changed.

@NonNull void
onRequestPermissionsResult(
    @NonNull int requestCode,
    @NonNull Array<@NonNull String> permissions,
    @NonNull Array<@NonNull int> grantResults
)

This method is deprecated.

use registerForActivityResult passing in a RequestMultiplePermissions object for the ActivityResultContract and handling the result in the callback.

@NonNull void

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

@NonNull 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.

@NonNull void

Called when the Fragment is visible to the user.

@NonNull void

Called when the Fragment is no longer started.

@NonNull void
onViewStateRestored(@Nullable Bundle savedInstanceState)

Called when all saved state has been restored into the view hierarchy of the fragment.

@NonNull void

Postpone the entering Fragment transition until startPostponedEnterTransition or FragmentManager#executePendingTransactions() has been called.

final @NonNull void
postponeEnterTransition(@NonNull long duration, @NonNull TimeUnit timeUnit)

Postpone the entering Fragment transition for a given amount of time and then call startPostponedEnterTransition.

final @NonNull ActivityResultLauncher<I>
<I, O> registerForActivityResult(
    @NonNull ActivityResultContract<I, O> contract,
    @NonNull ActivityResultCallback<O> callback
)

If the host of this fragment is an ActivityResultRegistryOwner the ActivityResultRegistry of the host will be used.

final @NonNull ActivityResultLauncher<I>
<I, O> registerForActivityResult(
    @NonNull ActivityResultContract<I, O> contract,
    @NonNull ActivityResultRegistry registry,
    @NonNull ActivityResultCallback<O> callback
)
@NonNull void

Registers a context menu to be shown for the given view (multiple views can show the context menu).

final @NonNull void
requestPermissions(
    @NonNull Array<@NonNull String> permissions,
    @NonNull int requestCode
)

This method is deprecated.

use registerForActivityResult passing in a RequestMultiplePermissions object for the ActivityResultContract and handling the result in the callback.

final @NonNull FragmentActivity

Return the FragmentActivity this fragment is currently associated with.

final @NonNull Bundle

Return the arguments supplied when the fragment was instantiated.

final @NonNull Context

Return the Context this fragment is currently associated with.

final @NonNull FragmentManager

This method is deprecated.

This has been renamed to getParentFragmentManager() to make it clear that you are accessing the FragmentManager that contains this Fragment and not the FragmentManager associated with child Fragments.

final @NonNull Object

Return the host object of this fragment.

final @NonNull Fragment

Returns the parent Fragment containing this Fragment.

final @NonNull View

Get the root view for the fragment's layout (the one returned by onCreateView).

@NonNull void

Sets whether the the exit transition and enter transition overlap or not.

@NonNull void

Sets whether the the return transition and reenter transition overlap or not.

@NonNull void
setArguments(@Nullable Bundle args)

Supply the construction arguments for this fragment.

@NonNull void
setEnterSharedElementCallback(@Nullable SharedElementCallback callback)

When custom transitions are used with Fragments, the enter transition callback is called when this Fragment is attached or detached when not popping the back stack.

@NonNull void

Sets the Transition that will be used to move Views into the initial scene.

@NonNull void
setExitSharedElementCallback(@Nullable SharedElementCallback callback)

When custom transitions are used with Fragments, the exit transition callback is called when this Fragment is attached or detached when popping the back stack.

@NonNull void

Sets the Transition that will be used to move Views out of the scene when the fragment is removed, hidden, or detached when not popping the back stack.

@NonNull void
setHasOptionsMenu(@NonNull boolean hasMenu)

Report that this fragment would like to participate in populating the options menu by receiving a call to onCreateOptionsMenu and related methods.

@NonNull void

Set the initial saved state that this Fragment should restore itself from when first being constructed, as returned by FragmentManager.saveFragmentInstanceState.

@NonNull void
setMenuVisibility(@NonNull boolean menuVisible)

Set a hint for whether this fragment's menu should be visible.

@NonNull void

Sets the Transition that will be used to move Views in to the scene when returning due to popping a back stack.

@NonNull void
setRetainInstance(@NonNull boolean retain)

This method is deprecated.

Instead of retaining the Fragment itself, use a non-retained Fragment and keep retained state in a ViewModel attached to that Fragment.

@NonNull void

Sets the Transition that will be used to move Views out of the scene when the Fragment is preparing to be removed, hidden, or detached because of popping the back stack.

@NonNull void

Sets the Transition that will be used for shared elements transferred into the content Scene.

@NonNull void

Sets the Transition that will be used for shared elements transferred back during a pop of the back stack.

@NonNull void
setTargetFragment(@Nullable Fragment fragment, @NonNull int requestCode)

This method is deprecated.

Instead of using a target fragment to pass results, the fragment requesting a result should use FragmentManager#setFragmentResultListener(String, LifecycleOwner, FragmentResultListener) to register a FragmentResultListener with a {@code * requestKey} using its parent fragment manager.

@NonNull void
setUserVisibleHint(@NonNull boolean isVisibleToUser)

This method is deprecated.

If you are manually calling this method, use FragmentTransaction#setMaxLifecycle(Fragment, Lifecycle.State) instead.

@NonNull boolean

Gets whether you should show UI with rationale before requesting a permission.

@NonNull void
startActivity(@NonNull Intent intent)

Call Activity#startActivity(Intent) from the fragment's containing Activity.

@NonNull void
startActivity(@NonNull Intent intent, @Nullable Bundle options)

Call Activity#startActivity(Intent, Bundle) from the fragment's containing Activity.

@NonNull void
startActivityForResult(@NonNull Intent intent, @NonNull int requestCode)

This method is deprecated.

use registerForActivityResult passing in a StartActivityForResult object for the ActivityResultContract.

@NonNull void
startActivityForResult(
    @NonNull Intent intent,
    @NonNull int requestCode,
    @Nullable Bundle options
)

This method is deprecated.

use registerForActivityResult passing in a StartActivityForResult object for the ActivityResultContract.

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

This method is deprecated.

use registerForActivityResult passing in a StartIntentSenderForResult object for the ActivityResultContract.

@NonNull void

Begin postponed transitions after postponeEnterTransition was called.

@NonNull String
@NonNull void

Prevents a context menu to be shown for the given view.

Public constructors

ListFragment

@NonNull
public ListFragment()

Public methods

getListAdapter

@Nullable
@NonNull
public ListAdapter getListAdapter()

Get the ListAdapter associated with this fragment's ListView.

See also
requireListAdapter

#requireListAdapter()

getListView

@NonNull
public ListView getListView()

Get the fragment's list view widget.

getSelectedItemId

@NonNull
public long getSelectedItemId()

Get the cursor row ID of the currently selected list item.

getSelectedItemPosition

@NonNull
public int getSelectedItemPosition()

Get the position of the currently selected list item.

onCreateView

@Nullable
@NonNull
public View onCreateView(
    @NonNull LayoutInflater inflater,
    @Nullable ViewGroup container,
    @Nullable Bundle savedInstanceState
)

Provide default implementation to return a simple list view. Subclasses can override to replace with their own layout. If doing so, the returned view hierarchy must have a ListView whose id is android.R.id.list and can optionally have a sibling view id android.R.id.empty that is to be shown when the list is empty.

If you are overriding this method with your own custom content, consider including the standard layout list_content in your layout file, so that you continue to retain all of the standard behavior of ListFragment. In particular, this is currently the only way to have the built-in indeterminant progress state be shown.

onDestroyView

@NonNull
public void onDestroyView()

Detach from list view.

onListItemClick

@NonNull
public void onListItemClick(
    @NonNull ListView l,
    @NonNull View v,
    @NonNull int position,
    @NonNull long id
)

This method will be called when an item in the list is selected. Subclasses should override. Subclasses can call getListView().getItemAtPosition(position) if they need to access the data associated with the selected item.

Parameters
@NonNull ListView l

The ListView where the click happened

@NonNull View v

The view that was clicked within the ListView

@NonNull int position

The position of the view in the list

@NonNull long id

The row id of the item that was clicked

onViewCreated

@NonNull
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)

Attach to list view once the view hierarchy has been created.

requireListAdapter

@NonNull
public final ListAdapter requireListAdapter()

Get the ListAdapter associated with this fragment's ListView.

Throws
java.lang.IllegalStateException

if no ListAdapter has been set.

See also
getListAdapter

#getListAdapter()

setEmptyText

@NonNull
public void setEmptyText(@Nullable CharSequence text)

The default content for a ListFragment has a TextView that can be shown when the list is empty. If you would like to have it shown, call this method to supply the text it should use.

setListAdapter

@NonNull
public void setListAdapter(@Nullable ListAdapter adapter)

Provide the cursor for the list view.

setListShown

@NonNull
public void setListShown(@NonNull boolean shown)

Control whether the list is being displayed. You can make it not displayed if you are waiting for the initial data to show in it. During this time an indeterminant progress indicator will be shown instead.

Applications do not normally need to use this themselves. The default behavior of ListFragment is to start with the list not being shown, only showing it once an adapter is given with setListAdapter. If the list at that point had not been shown, when it does get shown it will be do without the user ever seeing the hidden state.

Parameters
@NonNull boolean shown

If true, the list view is shown; if false, the progress indicator. The initial value is true.

setListShownNoAnimation

@NonNull
public void setListShownNoAnimation(@NonNull boolean shown)

Like setListShown, but no animation is used when transitioning from the previous state.

setSelection

@NonNull
public void setSelection(@NonNull int position)

Set the currently selected list item to the specified position with the adapter's data

Parameters
@NonNull int position