Class ActivityController<T extends Activity>
- java.lang.Object
-
- org.robolectric.android.controller.ComponentController<ActivityController<T>,T>
-
- org.robolectric.android.controller.ActivityController<T>
-
- Type Parameters:
T
- a class of the activity which is under control by this class.
- All Implemented Interfaces:
AutoCloseable
public class ActivityController<T extends Activity> extends ComponentController<ActivityController<T>,T> implements AutoCloseable
ActivityController provides low-level APIs to control activity's lifecycle.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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Transitions the underlying Activity to the 'destroyed' state by progressing through the appropriate lifecycle events.ActivityController<T>
configurationChange()
Applies the current system configuration to the Activity.ActivityController<T>
configurationChange(Configuration newConfiguration)
Performs a configuration change on the Activity.ActivityController<T>
create()
ActivityController<T>
create(Bundle bundle)
ActivityController<T>
destroy()
ActivityController<T>
newIntent(Intent intent)
static <T extends Activity>
ActivityController<T>of(T activity)
static <T extends Activity>
ActivityController<T>of(T activity, Intent intent)
static <T extends Activity>
ActivityController<T>of(T activity, Intent intent, Bundle activityOptions)
ActivityController<T>
pause()
ActivityController<T>
postCreate(Bundle bundle)
ActivityController<T>
postResume()
ActivityController<T>
recreate()
Recreates activity instance which is controlled by this ActivityController.ActivityController<T>
restart()
ActivityController<T>
restoreInstanceState(Bundle bundle)
ActivityController<T>
resume()
ActivityController<T>
saveInstanceState(Bundle outState)
ActivityController<T>
setup()
Calls the same lifecycle methods on the Activity called by Android the first time the Activity is created.ActivityController<T>
setup(Bundle savedInstanceState)
Calls the same lifecycle methods on the Activity called by Android when an Activity is restored from previously saved state.ActivityController<T>
start()
ActivityController<T>
stop()
ActivityController<T>
userLeaving()
ActivityController<T>
visible()
ActivityController<T>
windowFocusChanged(boolean hasFocus)
-
Methods inherited from class org.robolectric.android.controller.ComponentController
get, getIntent, invokeWhilePaused, invokeWhilePaused
-
-
-
-
Method Detail
-
of
public static <T extends Activity> ActivityController<T> of(T activity, Intent intent, @Nullable Bundle activityOptions)
-
of
public static <T extends Activity> ActivityController<T> of(T activity, Intent intent)
-
of
public static <T extends Activity> ActivityController<T> of(T activity)
-
create
public ActivityController<T> create(@Nullable Bundle bundle)
-
create
public ActivityController<T> create()
- Specified by:
create
in classComponentController<ActivityController<T extends Activity>,T extends Activity>
-
restart
public ActivityController<T> restart()
-
start
public ActivityController<T> start()
-
restoreInstanceState
public ActivityController<T> restoreInstanceState(Bundle bundle)
-
postCreate
public ActivityController<T> postCreate(@Nullable Bundle bundle)
-
resume
public ActivityController<T> resume()
-
postResume
public ActivityController<T> postResume()
-
visible
public ActivityController<T> visible()
-
windowFocusChanged
public ActivityController<T> windowFocusChanged(boolean hasFocus)
-
userLeaving
public ActivityController<T> userLeaving()
-
pause
public ActivityController<T> pause()
-
saveInstanceState
public ActivityController<T> saveInstanceState(Bundle outState)
-
stop
public ActivityController<T> stop()
-
destroy
public ActivityController<T> destroy()
- Specified by:
destroy
in classComponentController<ActivityController<T extends Activity>,T extends Activity>
-
setup
public ActivityController<T> setup()
Calls the same lifecycle methods on the Activity called by Android the first time the Activity is created.- Returns:
- Activity controller instance.
-
setup
public ActivityController<T> setup(Bundle savedInstanceState)
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
public ActivityController<T> newIntent(Intent intent)
-
configurationChange
public ActivityController<T> configurationChange()
Applies the current system configuration to the Activity.This can be used in conjunction with
RuntimeEnvironment.setQualifiers(String)
to simulate configuration changes.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.- Returns:
- ActivityController instance
-
configurationChange
public ActivityController<T> configurationChange(Configuration newConfiguration)
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.- Parameters:
newConfiguration
- The new configuration to be set.- Returns:
- ActivityController instance
-
recreate
public ActivityController<T> 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
-
-