Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

# ActivityMonitor

*** ** * ** ***

Kotlin \|[Java](https://developer.android.com/reference/android/app/Instrumentation.ActivityMonitor "View this page in Java")

```
open class ActivityMonitor
```

|---|---|
| [kotlin.Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html) ||
| ↳ | [android.app.Instrumentation.ActivityMonitor](https://developer.android.com/reference/kotlin/android/app/Instrumentation.ActivityMonitor#) |

Information about a particular kind of Intent that is being monitored. An instance of this class is added to the current instrumentation through #addMonitor; after being added, when a new activity is being started the monitor will be checked and, if matching, its hit count updated and (optionally) the call stopped and a canned result returned.

An ActivityMonitor can also be used to look for the creation of an activity, through the `https://developer.android.com/reference/kotlin/android/app/Instrumentation.ActivityMonitor#waitForActivity()` method. This will return after a matching activity has been created with that activity object.

## Summary

| Public constructors ||
|---|---|
| `https://developer.android.com/reference/kotlin/android/app/Instrumentation.ActivityMonitor#ActivityMonitor()()` Create a new ActivityMonitor that can be used for intercepting any activity to be started. |
| `https://developer.android.com/reference/kotlin/android/app/Instrumentation.ActivityMonitor#ActivityMonitor(android.content.IntentFilter,%20android.app.Instrumentation.ActivityResult,%20kotlin.Boolean)(which: https://developer.android.com/reference/kotlin/android/content/IntentFilter.html#!, result: https://developer.android.com/reference/kotlin/android/app/Instrumentation.ActivityResult!, block: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)` Create a new ActivityMonitor that looks for a particular kind of intent to be started. |
| `https://developer.android.com/reference/kotlin/android/app/Instrumentation.ActivityMonitor#ActivityMonitor(kotlin.String,%20android.app.Instrumentation.ActivityResult,%20kotlin.Boolean)(cls: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html!, result: https://developer.android.com/reference/kotlin/android/app/Instrumentation.ActivityResult!, block: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)` Create a new ActivityMonitor that looks for a specific activity class to be started. |

| Public methods ||
|---|---|
| [IntentFilter](https://developer.android.com/reference/kotlin/android/content/IntentFilter.html#)! | `https://developer.android.com/reference/kotlin/android/app/Instrumentation.ActivityMonitor#getFilter()()` Retrieve the filter associated with this ActivityMonitor. |
| [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html) | `https://developer.android.com/reference/kotlin/android/app/Instrumentation.ActivityMonitor#getHits()()` Retrieve the number of times the monitor has been hit so far. |
| [Activity](https://developer.android.com/reference/kotlin/android/app/Activity)! | `https://developer.android.com/reference/kotlin/android/app/Instrumentation.ActivityMonitor#getLastActivity()()` Retrieve the most recent activity class that was seen by this monitor. |
| [Instrumentation.ActivityResult](https://developer.android.com/reference/kotlin/android/app/Instrumentation.ActivityResult)! | `https://developer.android.com/reference/kotlin/android/app/Instrumentation.ActivityMonitor#getResult()()` Retrieve the result associated with this ActivityMonitor, or null if none. |
| [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) | `https://developer.android.com/reference/kotlin/android/app/Instrumentation.ActivityMonitor#isBlocking()()` Check whether this monitor blocks activity starts (not allowing the actual activity to run) or allows them to execute normally. |
| open [Instrumentation.ActivityResult](https://developer.android.com/reference/kotlin/android/app/Instrumentation.ActivityResult)! | `https://developer.android.com/reference/kotlin/android/app/Instrumentation.ActivityMonitor#onStartActivity(android.content.Intent)(intent: https://developer.android.com/reference/kotlin/android/content/Intent.html#!)` Used for intercepting any started activity. |
| [Activity](https://developer.android.com/reference/kotlin/android/app/Activity)! | `https://developer.android.com/reference/kotlin/android/app/Instrumentation.ActivityMonitor#waitForActivity()()` Block until an Activity is created that matches this monitor, returning the resulting activity. |
| [Activity](https://developer.android.com/reference/kotlin/android/app/Activity)! | `https://developer.android.com/reference/kotlin/android/app/Instrumentation.ActivityMonitor#waitForActivityWithTimeout(kotlin.Long)(timeOut: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-long/index.html)` Block until an Activity is created that matches this monitor, returning the resulting activity or till the timeOut period expires. |

## Public constructors

### ActivityMonitor

Added in [API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
ActivityMonitor()
```

Create a new ActivityMonitor that can be used for intercepting any activity to be started.

When an activity is started, `https://developer.android.com/reference/kotlin/android/app/Instrumentation.ActivityMonitor#onStartActivity(android.content.Intent)` will be called on instances created using this constructor to see if it is a hit.
**See Also**

- `https://developer.android.com/reference/kotlin/android/app/Instrumentation.ActivityMonitor#onStartActivity(android.content.Intent)`

### ActivityMonitor

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
ActivityMonitor(
    which: IntentFilter!, 
    result: Instrumentation.ActivityResult!, 
    block: Boolean)
```

Create a new ActivityMonitor that looks for a particular kind of intent to be started.

| Parameters ||
|---|---|
| `which` | [IntentFilter](https://developer.android.com/reference/kotlin/android/content/IntentFilter.html#)!: The set of intents this monitor is responsible for. |
| `result` | [Instrumentation.ActivityResult](https://developer.android.com/reference/kotlin/android/app/Instrumentation.ActivityResult)!: A canned result to return if the monitor is hit; can be null. |
| `block` | [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html): Controls whether the monitor should block the activity start (returning its canned result) or let the call proceed. |

**See Also**

### ActivityMonitor

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
ActivityMonitor(
    cls: String!, 
    result: Instrumentation.ActivityResult!, 
    block: Boolean)
```

Create a new ActivityMonitor that looks for a specific activity class to be started.

| Parameters ||
|---|---|
| `cls` | [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)!: The activity class this monitor is responsible for. |
| `result` | [Instrumentation.ActivityResult](https://developer.android.com/reference/kotlin/android/app/Instrumentation.ActivityResult)!: A canned result to return if the monitor is hit; can be null. |
| `block` | [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html): Controls whether the monitor should block the activity start (returning its canned result) or let the call proceed. |

**See Also**

## Public methods

### getFilter

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
fun getFilter(): IntentFilter!
```

Retrieve the filter associated with this ActivityMonitor.

### getHits

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
fun getHits(): Int
```

Retrieve the number of times the monitor has been hit so far.

### getLastActivity

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
fun getLastActivity(): Activity!
```

Retrieve the most recent activity class that was seen by this monitor.

### getResult

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
fun getResult(): Instrumentation.ActivityResult!
```

Retrieve the result associated with this ActivityMonitor, or null if none.

### isBlocking

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
fun isBlocking(): Boolean
```

Check whether this monitor blocks activity starts (not allowing the actual activity to run) or allows them to execute normally.

### onStartActivity

Added in [API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
open fun onStartActivity(intent: Intent!): Instrumentation.ActivityResult!
```

Used for intercepting any started activity.

A non-null return value here will be considered a hit for this monitor. By default this will return `null` and subclasses can override this to return a non-null value if the intent needs to be intercepted.

Whenever a new activity is started, this method will be called on instances created using `https://developer.android.com/reference/kotlin/android/app/Instrumentation.ActivityMonitor#ActivityMonitor()` to check if there is a match. In case of a match, the activity start will be blocked and the returned result will be used.

| Parameters ||
|---|---|
| `intent` | [Intent](https://developer.android.com/reference/kotlin/android/content/Intent.html#)!: The intent used for starting the activity. |

| Return ||
|---|---|
| `https://developer.android.com/reference/kotlin/android/app/Instrumentation.ActivityResult!` | The `https://developer.android.com/reference/kotlin/android/app/Instrumentation.ActivityResult` that needs to be used in case of a match. |

### waitForActivity

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
fun waitForActivity(): Activity!
```

Block until an Activity is created that matches this monitor, returning the resulting activity.

| Return ||
|---|---|
| `https://developer.android.com/reference/kotlin/android/app/Activity!` | Activity |

### waitForActivityWithTimeout

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
fun waitForActivityWithTimeout(timeOut: Long): Activity!
```

Block until an Activity is created that matches this monitor, returning the resulting activity or till the timeOut period expires. If the timeOut expires before the activity is started, return null.

| Parameters ||
|---|---|
| `timeOut` | [Long](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-long/index.html): Time to wait in milliseconds before the activity is created. |

| Return ||
|---|---|
| `https://developer.android.com/reference/kotlin/android/app/Activity!` | Activity |