Save the date! Android Dev Summit is coming to Sunnyvale, CA on Oct 23-24, 2019.

androidx.fragment.app

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.

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.

Extension functions summary

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.

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.

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


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.

commit

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

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

The transaction will be completed by calling FragmentTransaction.commit unless allowStateLoss is set to true in which case FragmentTransaction.commitAllowingStateLoss will be used.

commitNow

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

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

The transaction will be completed by calling FragmentTransaction.commitNow unless allowStateLoss is set to true in which case FragmentTransaction.commitNowAllowingStateLoss will be used.

createViewModelLazy

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

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

transaction

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

Deprecated.

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

One of four commit functions will be used based on the values of now and allowStateLoss:

    |  now  |  allowStateLoss  | Method                         |
    | ----- | ---------------- | ------------------------------ |
    | false | false            |  commit()                      |
    | false | true             |  commitAllowingStateLoss()     |
    | true  | false            |  commitNow()                   |
    | true  | true             |  commitNowAllowingStateLoss()  |

viewModels

@MainThread inline fun <reified VM : ViewModel> Fragment.viewModels(noinline ownerProducer: () -> ViewModelStoreOwner = { this }, noinline factoryProducer: () -> ViewModelProvider.Factory = null): Lazy<VM>

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


class MyFragment : Fragment() {
    val viewmodel: NYViewModel by viewmodels()
}

Custom ViewModelProvider.Factory can be defined via factoryProducer parameter, factory returned by it will be used to create ViewModel:


class MyFragment : Fragment() {
    val viewmodel: MYViewModel by viewmodels { myFactory }
}

Default scope may be overridden with parameter ownerProducer:


class MyFragment : Fragment() {
    val viewmodel: MYViewModel by viewmodels ({requireParentFragment()})
}

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.