Google 致力于为黑人社区推动种族平等。查看具体举措

androidx.fragment.app

Interfaces

BackStackEntry

Representation of an entry on the fragment back stack, as created with FragmentTransaction.addToBackStack().

OnBackStackChangedListener

Interface to watch for changes to the back stack.

FragmentOnAttachListener

Listener for receiving a callback immediately following Fragment#onAttach(Context).

FragmentResultListener

Listener for handling fragment results.

FragmentResultOwner

A class that manages passing data between fragments.

Classes

DialogFragment

Static library support version of the framework's android.app.DialogFragment.

Fragment

Static library support version of the framework's android.app.Fragment.

FragmentActivity

Base class for activities that want to use the support-based Fragments.

FragmentContainer

Callbacks to a Fragment's container.

FragmentContainerView

FragmentContainerView is a customized Layout designed specifically for Fragments.

FragmentController

Provides integration points with a FragmentManager for a fragment host.

FragmentFactory

Interface used to control the instantiation of Fragment instances.

FragmentHostCallback

Integration points with the Fragment host.

FragmentManager

Static library support version of the framework's android.app.FragmentManager.

FragmentManagerNonConfig

FragmentManagerNonConfig stores the retained instance fragments across activity recreation events.

FragmentPagerAdapter

Implementation of PagerAdapter that represents each page as a Fragment that is persistently kept in the fragment manager as long as the user can return to the page.

FragmentStatePagerAdapter

Implementation of PagerAdapter that uses a Fragment to manage each page.

FragmentTabHost

Special TabHost that allows the use of Fragment objects for its tab content.

FragmentTransaction

Static library support version of the framework's android.app.FragmentTransaction.

ListFragment

Static library support version of the framework's android.app.ListFragment.

Annotations

FragmentStateManagerControl

Extension functions summary

For android.view.View
F

Find a Fragment associated with a View.

For Fragment
Lazy<VM>
Fragment.activityViewModels(noinline factoryProducer: () -> ViewModelProvider.Factory = null)

Returns a property delegate to access parent activity's ViewModel, if factoryProducer is specified then ViewModelProvider.Factory returned by it will be used to create ViewModel first time.

Unit

Clears the stored result for the given requestKey.

Unit

Clears the stored FragmentResultListener for the given requestKey.

Lazy<VM>
Fragment.createViewModelLazy(viewModelClass: KClass<VM>, storeProducer: () -> ViewModelStore, factoryProducer: () -> ViewModelProvider.Factory = null)

Helper method for creation of ViewModelLazy, that resolves null passed as factoryProducer to default factory.

Unit
Fragment.setFragmentResult(requestKey: String, result: Bundle)

Sets the given result for the requestKey.

Unit
Fragment.setFragmentResultListener(requestKey: String, listener: (requestKey: String, bundle: Bundle) -> Unit)

Sets the FragmentResultListener for a given requestKey.

Lazy<VM>
Fragment.viewModels(noinline ownerProducer: () -> ViewModelStoreOwner = { this }, noinline factoryProducer: () -> ViewModelProvider.Factory = null)

Returns a property delegate to access ViewModel by default scoped to this Fragment:

For FragmentTransaction
FragmentTransaction
FragmentTransaction.add(@IdRes containerViewId: Int, tag: String? = null, args: Bundle? = null)

Add a fragment to the associated FragmentManager, inflating the Fragment's view into the container view specified by containerViewId, to later retrieve via FragmentManager.findFragmentById.

FragmentTransaction
FragmentTransaction.add(tag: String, args: Bundle? = null)

Add a fragment to the associated FragmentManager without adding the Fragment to any container view.

FragmentTransaction
FragmentTransaction.replace(@IdRes containerViewId: Int, tag: String? = null, args: Bundle? = null)

Replace an existing fragment that was added to a container.

For FragmentManager
Unit
FragmentManager.commit(allowStateLoss: Boolean = false, body: FragmentTransaction.() -> Unit)

Run body in a FragmentTransaction which is automatically committed if it completes without exception.

Unit
FragmentManager.commitNow(allowStateLoss: Boolean = false, body: FragmentTransaction.() -> Unit)

Run body in a FragmentTransaction which is automatically committed if it completes without exception.

Unit
FragmentManager.transaction(now: Boolean = false, allowStateLoss: Boolean = false, body: FragmentTransaction.() -> Unit)

Run body in a FragmentTransaction which is automatically committed if it completes without exception.

Extension functions

activityViewModels

@MainThread inline fun <reified VM : ViewModel> Fragment.activityViewModels(noinline factoryProducer: () -> ViewModelProvider.Factory = null): Lazy<VM>

Returns a property delegate to access parent activity's ViewModel, if factoryProducer is specified then ViewModelProvider.Factory returned by it will be used to create ViewModel first time. Otherwise, the activity's androidx.activity.ComponentActivity.getDefaultViewModelProviderFactory will be used.

class MyFragment : Fragment() {
    val viewmodel: MyViewModel by activityViewModels()
}

This property can be accessed only after this Fragment is attached i.e., after Fragment.onAttach, and access prior to that will result in IllegalArgumentException.

add

inline fun <reified F : Fragment> FragmentTransaction.add(
    @IdRes containerViewId: Int,
    tag: String? = null,
    args: Bundle? = null
):