Class ActivityController<T extends Activity>
- Type Parameters:
T
- a class of the activity which is under control by this class.
- All Implemented Interfaces:
AutoCloseable
Using ActivityController directly from your tests is strongly discouraged. You have to call
all the lifecycle callback methods (create, postCreate, start, ...) in the same manner as the
Android framework by yourself otherwise you'll see fidelity issues. Consider using androidx.test.core.app.ActivityScenario
instead, which provides higher-level, streamlined APIs
to control the lifecycle and it works with instrumentation tests too.
-
Field Summary
Fields inherited from class org.robolectric.android.controller.ComponentController
attached, component, intent, myself, shadowMainLooper
-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Transitions the underlying Activity to the 'destroyed' state by progressing through the appropriate lifecycle events.Performs a configuration change on the Activity.configurationChange
(Configuration newConfiguration) Performs a configuration change on the Activity.configurationChange
(Configuration newConfiguration, DisplayMetrics newMetrics, int changedConfig) Performs a configuration change on the Activity.create()
destroy()
static <T extends Activity>
ActivityController<T>of
(T activity) static <T extends Activity>
ActivityController<T>static <T extends Activity>
ActivityController<T>pause()
postCreate
(Bundle bundle) recreate()
Recreates activity instance which is controlled by this ActivityController.restart()
restoreInstanceState
(Bundle bundle) resume()
saveInstanceState
(Bundle outState) setup()
Calls the same lifecycle methods on the Activity called by Android the first time the Activity is created.Calls the same lifecycle methods on the Activity called by Android when an Activity is restored from previously saved state.start()
stop()
topActivityResumed
(boolean isTop) Calls the same lifecycle methods on the Activity called by Android when an Activity is the top most resumed activity on Q+.visible()
windowFocusChanged
(boolean hasFocus) Methods inherited from class org.robolectric.android.controller.ComponentController
get, getIntent, invokeWhilePaused, invokeWhilePaused
-
Method Details
-
of
public static <T extends Activity> ActivityController<T> of(T activity, Intent intent, @Nullable Bundle activityOptions) -
of
-
of
-
create
-
create
- Specified by:
create
in classComponentController<ActivityController<T extends Activity>,
T extends Activity>
-
restart
-
start
-
restoreInstanceState
-
postCreate
-
resume
-
postResume
-
topActivityResumed
Calls the same lifecycle methods on the Activity called by Android when an Activity is the top most resumed activity on Q+. -
visible
-
windowFocusChanged
-
userLeaving
-
pause
-
saveInstanceState
-
stop
-
destroy
- Specified by:
destroy
in classComponentController<ActivityController<T extends Activity>,
T extends Activity>
-
setup
Calls the same lifecycle methods on the Activity called by Android the first time the Activity is created.- Returns:
- Activity controller instance.
-
setup
Calls the same lifecycle methods on the Activity called by Android when an Activity is restored from previously saved state.- Parameters:
savedInstanceState
- Saved instance state.- Returns:
- Activity controller instance.
-
newIntent
-
configurationChange
Performs a configuration change on the Activity. SeeconfigurationChange(Configuration, DisplayMetrics, int)
. The configuration is taken from the application's configuration.Generally this method should be avoided due to the way Robolectric shares the application context with activitys by default, this will result in the configuration diff producing no indicated change and Robolectric will not recreate the activity. Instead prefer to use
configurationChange(Configuration, DisplayMetrics, int)
and provide an explicit configuration and diff. -
configurationChange
Performs a configuration change on the Activity. SeeconfigurationChange(Configuration, DisplayMetrics, int)
. The changed configuration is calculated based on the activity's existing configuration.When using
RuntimeEnvironment.setQualifiers(String)
prefer to use theconfigurationChange(Configuration, DisplayMetrics, int)
method and calculate the configuration diff manually, due to the way Robolectric uses the application context for activitys by default the configuration diff will otherwise be incorrectly calculated and the activity will not get recreqted if it doesn't handle configuration change. -
configurationChange
public ActivityController<T> configurationChange(Configuration newConfiguration, DisplayMetrics newMetrics, int changedConfig) Performs a configuration change on the Activity.If the activity is configured to handle changes without being recreated,
Activity.onConfigurationChanged(Configuration)
will be called. Otherwise, the activity is recreated as described here.Typically configuration should be applied using
RuntimeEnvironment.setQualifiers(java.lang.String)
and then propagated to the activity controller, e.g.Resources resources = RuntimeEnvironment.getApplication().getResources(); Configuration oldConfig = new Configuration(resources.getConfiguration()); RuntimeEnvironment.setQualifiers("+ar-rXB"); Configuration newConfig = resources.getConfiguration(); activityController.configurationChange( newConfig, resources.getDisplayMetrics(), oldConfig.diff(newConfig));
- Parameters:
newConfiguration
- The new configuration to be set.changedConfig
- The changed configuration properties bitmask (e.g. the result of callingConfiguration.diff(Configuration)
). This will be used to determine whether the activity handles the configuration change or not, and whether it must be recreated.- Returns:
- ActivityController instance
-
recreate
Recreates activity instance which is controlled by this ActivityController. NonConfigurationInstances and savedInstanceStateBundle are properly passed into a new instance. After the recreation, it brings back its lifecycle state to the original state. The activity should not be destroyed yet. -
close
public void close()Transitions the underlying Activity to the 'destroyed' state by progressing through the appropriate lifecycle events. It frees up any resources and makes the Activity eligible for GC.- Specified by:
close
in interfaceAutoCloseable
-