FragmentScenario
public
final
class
FragmentScenario
extends Object
java.lang.Object | |
↳ | androidx.fragment.app.testing.FragmentScenario<F extends androidx.fragment.app.Fragment> |
FragmentScenario provides API to start and drive a Fragment's lifecycle state for testing. It works with arbitrary fragments and works consistently across different versions of the Android framework.
FragmentScenario only supports androidx.fragment.app.Fragment
. If you are using
a deprecated fragment class such as android.support.v4.app.Fragment
or
android.app.Fragment
, please update your code to
androidx.fragment.app.Fragment
.
If your testing Fragment has a dependency to specific theme such as Theme.AppCompat
,
use the theme ID parameter in launch(Class
method.
See also:
Summary
Nested classes | |
---|---|
interface |
FragmentScenario.FragmentAction<F extends Fragment>
FragmentAction interface should be implemented by any class whose instances are intended to be executed by the main thread. |
Public methods | |
---|---|
static
<F extends Fragment>
FragmentScenario<F>
|
launch(Class<F> fragmentClass, Bundle fragmentArgs, FragmentFactory factory)
Launches a Fragment with given arguments hosted by an empty |
static
<F extends Fragment>
FragmentScenario<F>
|
launch(Class<F> fragmentClass)
Launches a Fragment hosted by an empty |
static
<F extends Fragment>
FragmentScenario<F>
|
launch(Class<F> fragmentClass, Bundle fragmentArgs, int themeResId, FragmentFactory factory)
Launches a Fragment with given arguments hosted by an empty |
static
<F extends Fragment>
FragmentScenario<F>
|
launch(Class<F> fragmentClass, Bundle fragmentArgs)
Launches a Fragment with given arguments hosted by an empty |
static
<F extends Fragment>
FragmentScenario<F>
|
launch(Class<F> fragmentClass, Bundle fragmentArgs, int themeResId, Lifecycle.State initialState, FragmentFactory factory)
Launches a Fragment with given arguments hosted by an empty |
static
<F extends Fragment>
FragmentScenario<F>
|
launchInContainer(Class<F> fragmentClass)
Launches a Fragment in the Activity's root view container |
static
<F extends Fragment>
FragmentScenario<F>
|
launchInContainer(Class<F> fragmentClass, Bundle fragmentArgs, int themeResId, Lifecycle.State initialState, FragmentFactory factory)
Launches a Fragment in the Activity's root view container |
static
<F extends Fragment>
FragmentScenario<F>
|
launchInContainer(Class<F> fragmentClass, Bundle fragmentArgs, FragmentFactory factory)
Launches a Fragment in the Activity's root view container |
static
<F extends Fragment>
FragmentScenario<F>
|
launchInContainer(Class<F> fragmentClass, Bundle fragmentArgs)
Launches a Fragment in the Activity's root view container |
static
<F extends Fragment>
FragmentScenario<F>
|
launchInContainer(Class<F> fragmentClass, Bundle fragmentArgs, int themeResId, FragmentFactory factory)
Launches a Fragment in the Activity's root view container |
FragmentScenario<F>
|
moveToState(Lifecycle.State newState)
Moves Fragment state to a new state. |
FragmentScenario<F>
|
onFragment(FragmentAction<F> action)
Runs a given |
FragmentScenario<F>
|
recreate()
Recreates the host Activity. |
Inherited methods | |
---|---|
Public methods
launch
public static FragmentScenario<F> launch (Class<F> fragmentClass, Bundle fragmentArgs, FragmentFactory factory)
Launches a Fragment with given arguments hosted by an empty FragmentActivity
using
the given FragmentFactory
and waits for it to reach the resumed state.
This method cannot be called from the main thread.
Parameters | |
---|---|
fragmentClass |
Class : a fragment class to instantiate |
fragmentArgs |
Bundle : a bundle to passed into fragment |
factory |
FragmentFactory : a fragment factory to use or null to use default factory
|
Returns | |
---|---|
FragmentScenario<F> |
launch
public static FragmentScenario<F> launch (Class<F> fragmentClass)
Launches a Fragment hosted by an empty FragmentActivity
and waits for it to reach
the resumed state.
Parameters | |
---|---|
fragmentClass |
Class : a fragment class to instantiate
|
Returns | |
---|---|
FragmentScenario<F> |
launch
public static FragmentScenario<F> launch (Class<F> fragmentClass, Bundle fragmentArgs, int themeResId, FragmentFactory factory)
Launches a Fragment with given arguments hosted by an empty FragmentActivity
themed
by themeResId
, using the given FragmentFactory
and waits for it to reach the
resumed state.
This method cannot be called from the main thread.
Parameters | |
---|---|
fragmentClass |
Class : a fragment class to instantiate |
fragmentArgs |
Bundle : a bundle to passed into fragment |
themeResId |
int : a style resource id to be set to the host activity's theme |
factory |
FragmentFactory : a fragment factory to use or null to use default factory
|
Returns | |
---|---|
FragmentScenario<F> |
launch
public static FragmentScenario<F> launch (Class<F> fragmentClass, Bundle fragmentArgs)
Launches a Fragment with given arguments hosted by an empty FragmentActivity
and
waits for it to reach the resumed state.
This method cannot be called from the main thread.
Parameters | |
---|---|
fragmentClass |
Class : a fragment class to instantiate |
fragmentArgs |
Bundle : a bundle to passed into fragment
|
Returns | |
---|---|
FragmentScenario<F> |
launch
public static FragmentScenario<F> launch (Class<F> fragmentClass, Bundle fragmentArgs, int themeResId, Lifecycle.State initialState, FragmentFactory factory)
Launches a Fragment with given arguments hosted by an empty FragmentActivity
themed
by themeResId
, using the given FragmentFactory
and waits for it to reach
initialState
.
This method cannot be called from the main thread.
Parameters | |
---|---|
fragmentClass |
Class : a fragment class to instantiate |
fragmentArgs |
Bundle : a bundle to passed into fragment |
themeResId |
int : a style resource id to be set to the host activity's theme |
initialState |
Lifecycle.State : The initial Lifecycle.State . This must be one of
CREATED , STARTED , and
RESUMED . |
factory |
FragmentFactory : a fragment factory to use or null to use default factory
|
Returns | |
---|---|
FragmentScenario<F> |
launchInContainer
public static FragmentScenario<F> launchInContainer (Class<F> fragmentClass)
Launches a Fragment in the Activity's root view container android.R.id.content
,
hosted by an empty FragmentActivity
and waits for it to reach the resumed state.
This method cannot be called from the main thread.
Parameters | |
---|---|
fragmentClass |
Class : a fragment class to instantiate
|
Returns | |
---|---|
FragmentScenario<F> |
launchInContainer
public static FragmentScenario<F> launchInContainer (Class<F> fragmentClass, Bundle fragmentArgs, int themeResId, Lifecycle.State initialState, FragmentFactory factory)
Launches a Fragment in the Activity's root view container android.R.id.content
, with
given arguments hosted by an empty FragmentActivity
themed by themeResId
,
using the given FragmentFactory
and waits for it to reach initialState
.
This method cannot be called from the main thread.
Parameters | |
---|---|
fragmentClass |
Class : a fragment class to instantiate |
fragmentArgs |
Bundle : a bundle to passed into fragment |
themeResId |
int : a style resource id to be set to the host activity's theme |
initialState |
Lifecycle.State : The initial Lifecycle.State . This must be one of
CREATED , STARTED , and
RESUMED . |
factory |
FragmentFactory : a fragment factory to use or null to use default factory
|
Returns | |
---|---|
FragmentScenario<F> |
launchInContainer
public static FragmentScenario<F> launchInContainer (Class<F> fragmentClass, Bundle fragmentArgs, FragmentFactory factory)
Launches a Fragment in the Activity's root view container android.R.id.content
, with
given arguments hosted by an empty FragmentActivity
using the given
FragmentFactory
and waits for it to reach the resumed state.
This method cannot be called from the main thread.
Parameters | |
---|---|
fragmentClass |
Class : a fragment class to instantiate |
fragmentArgs |
Bundle : a bundle to passed into fragment |
factory |
FragmentFactory : a fragment factory to use or null to use default factory
|
Returns | |
---|---|
FragmentScenario<F> |
launchInContainer
public static FragmentScenario<F> launchInContainer (Class<F> fragmentClass, Bundle fragmentArgs)
Launches a Fragment in the Activity's root view container android.R.id.content
, with
given arguments hosted by an empty FragmentActivity
and waits for it to reach the
resumed state.
This method cannot be called from the main thread.
Parameters | |
---|---|
fragmentClass |
Class : a fragment class to instantiate |
fragmentArgs |
Bundle : a bundle to passed into fragment
|
Returns | |
---|---|
FragmentScenario<F> |
launchInContainer
public static FragmentScenario<F> launchInContainer (Class<F> fragmentClass, Bundle fragmentArgs, int themeResId, FragmentFactory factory)
Launches a Fragment in the Activity's root view container android.R.id.content
, with
given arguments hosted by an empty FragmentActivity
themed by themeResId
,
using the given FragmentFactory
and waits for it to reach the resumed state.
This method cannot be called from the main thread.
Parameters | |
---|---|
fragmentClass |
Class : a fragment class to instantiate |
fragmentArgs |
Bundle : a bundle to passed into fragment |
themeResId |
int : a style resource id to be set to the host activity's theme |
factory |
FragmentFactory : a fragment factory to use or null to use default factory
|
Returns | |
---|---|
FragmentScenario<F> |
moveToState
public FragmentScenario<F> moveToState (Lifecycle.State newState)
Moves Fragment state to a new state.
If a new state and current state are the same, this method does nothing. It accepts
CREATED
, STARTED
, RESUMED
,
and DESTROYED
. DESTROYED
is a terminal state.
You cannot move to any other state after the Fragment reaches that state.
This method cannot be called from the main thread.
Parameters | |
---|---|
newState |
Lifecycle.State |
Returns | |
---|---|
FragmentScenario<F> |
onFragment
public FragmentScenario<F> onFragment (FragmentAction<F> action)
Runs a given action
on the current Activity's main thread.
Note that you should never keep Fragment reference passed into your action
because it can be recreated at anytime during state transitions.
Throwing an exception from action
makes the host Activity crash. You can
inspect the exception in logcat outputs.
This method cannot be called from the main thread.
Parameters | |
---|---|
action |
FragmentAction |
Returns | |
---|---|
FragmentScenario<F> |
recreate
public FragmentScenario<F> recreate ()
Recreates the host Activity.
After this method call, it is ensured that the Fragment state goes back to the same state as its previous state.
This method cannot be called from the main thread.
Returns | |
---|---|
FragmentScenario<F> |
Content and code samples on this page are subject to the licenses described in the Content License. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2020-09-30 UTC.