# Instrumentation.ActivityMonitor

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Summary:[Ctors](https://developer.android.com/reference/android/app/Instrumentation.ActivityMonitor#pubctors)\|[Methods](https://developer.android.com/reference/android/app/Instrumentation.ActivityMonitor#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/android/app/Instrumentation.ActivityMonitor#inhmethods)  

# Instrumentation.ActivityMonitor

*** ** * ** ***

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

`
public
static


class
Instrumentation.ActivityMonitor
`  
`

extends `[Object](https://developer.android.com/reference/java/lang/Object)`


``


`

|---|---------------------------------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.app.Instrumentation.ActivityMonitor |

<br />

*** ** * ** ***

Information about a particular kind of Intent that is being monitored. An instance of this class is added to the current instrumentation through[Instrumentation.addMonitor(ActivityMonitor)](https://developer.android.com/reference/android/app/Instrumentation#addMonitor(android.app.Instrumentation.ActivityMonitor)); 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[waitForActivity()](https://developer.android.com/reference/android/app/Instrumentation.ActivityMonitor#waitForActivity())method. This will return after a matching activity has been created with that activity object.

## Summary

|                                                                                                                                                                                                                                                                  ### Public constructors                                                                                                                                                                                                                                                                  ||
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[ActivityMonitor](https://developer.android.com/reference/android/app/Instrumentation.ActivityMonitor#ActivityMonitor())`() ` Create a new ActivityMonitor that can be used for intercepting any activity to be started.                                                                                                                                                                                                                                                                                                                            |
| ` `[ActivityMonitor](https://developer.android.com/reference/android/app/Instrumentation.ActivityMonitor#ActivityMonitor(android.content.IntentFilter,%20android.app.Instrumentation.ActivityResult,%20boolean))`(`[IntentFilter](https://developer.android.com/reference/android/content/IntentFilter)` which, `[Instrumentation.ActivityResult](https://developer.android.com/reference/android/app/Instrumentation.ActivityResult)` result, boolean block) ` Create a new ActivityMonitor that looks for a particular kind of intent to be started. |
| ` `[ActivityMonitor](https://developer.android.com/reference/android/app/Instrumentation.ActivityMonitor#ActivityMonitor(java.lang.String,%20android.app.Instrumentation.ActivityResult,%20boolean))`(`[String](https://developer.android.com/reference/java/lang/String)` cls, `[Instrumentation.ActivityResult](https://developer.android.com/reference/android/app/Instrumentation.ActivityResult)` result, boolean block) ` Create a new ActivityMonitor that looks for a specific activity class to be started.                                   |

|                                                                                                                                                                                                            ### Public methods                                                                                                                                                                                                            ||
|-------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` final `[IntentFilter](https://developer.android.com/reference/android/content/IntentFilter)                                 | ` `[getFilter](https://developer.android.com/reference/android/app/Instrumentation.ActivityMonitor#getFilter())`() ` Retrieve the filter associated with this ActivityMonitor.                                                                                                                            |
| ` final int`                                                                                                                  | ` `[getHits](https://developer.android.com/reference/android/app/Instrumentation.ActivityMonitor#getHits())`() ` Retrieve the number of times the monitor has been hit so far.                                                                                                                            |
| ` final `[Activity](https://developer.android.com/reference/android/app/Activity)                                             | ` `[getLastActivity](https://developer.android.com/reference/android/app/Instrumentation.ActivityMonitor#getLastActivity())`() ` Retrieve the most recent activity class that was seen by this monitor.                                                                                                   |
| ` final `[Instrumentation.ActivityResult](https://developer.android.com/reference/android/app/Instrumentation.ActivityResult) | ` `[getResult](https://developer.android.com/reference/android/app/Instrumentation.ActivityMonitor#getResult())`() ` Retrieve the result associated with this ActivityMonitor, or null if none.                                                                                                           |
| ` final boolean`                                                                                                              | ` `[isBlocking](https://developer.android.com/reference/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.                                                    |
| ` `[Instrumentation.ActivityResult](https://developer.android.com/reference/android/app/Instrumentation.ActivityResult)       | ` `[onStartActivity](https://developer.android.com/reference/android/app/Instrumentation.ActivityMonitor#onStartActivity(android.content.Intent))`(`[Intent](https://developer.android.com/reference/android/content/Intent)` intent) ` Used for intercepting any started activity.                       |
| ` final `[Activity](https://developer.android.com/reference/android/app/Activity)                                             | ` `[waitForActivity](https://developer.android.com/reference/android/app/Instrumentation.ActivityMonitor#waitForActivity())`() ` Block until an Activity is created that matches this monitor, returning the resulting activity.                                                                          |
| ` final `[Activity](https://developer.android.com/reference/android/app/Activity)                                             | ` `[waitForActivityWithTimeout](https://developer.android.com/reference/android/app/Instrumentation.ActivityMonitor#waitForActivityWithTimeout(long))`(long timeOut) ` Block until an Activity is created that matches this monitor, returning the resulting activity or till the timeOut period expires. |

| ### Inherited methods |
|-----------------------|---|
| From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                      | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                           | | ` void`                                                                        | ` `[finalize](https://developer.android.com/reference/java/lang/Object#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.                                                          | | ` final `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this`Object`.                                                                                                                                                | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                 | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                            | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                         | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                            | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long,%20int))`(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed. | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long))`(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed.                   | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait())`() ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*.                                                                                             | ||

## Public constructors

### ActivityMonitor

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

```
public ActivityMonitor ()
```

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

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

<br />

**See also:**

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

### ActivityMonitor

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

```
public ActivityMonitor (IntentFilter which, 
                Instrumentation.ActivityResult result, 
                boolean block)
```

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

<br />

|                                                                   Parameters                                                                    ||
|----------|---------------------------------------------------------------------------------------------------------------------------------------|
| `which`  | `IntentFilter`: The set of intents this monitor is responsible for. <br />                                                            |
| `result` | `Instrumentation.ActivityResult`: A canned result to return if the monitor is hit; can be null. <br />                                |
| `block`  | `boolean`: Controls whether the monitor should block the activity start (returning its canned result) or let the call proceed. <br /> |

**See also:**

- [Instrumentation.addMonitor(ActivityMonitor)](https://developer.android.com/reference/android/app/Instrumentation#addMonitor(android.app.Instrumentation.ActivityMonitor))  

### ActivityMonitor

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

```
public ActivityMonitor (String cls, 
                Instrumentation.ActivityResult result, 
                boolean block)
```

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

<br />

|                                                                   Parameters                                                                    ||
|----------|---------------------------------------------------------------------------------------------------------------------------------------|
| `cls`    | `String`: The activity class this monitor is responsible for. <br />                                                                  |
| `result` | `Instrumentation.ActivityResult`: A canned result to return if the monitor is hit; can be null. <br />                                |
| `block`  | `boolean`: Controls whether the monitor should block the activity start (returning its canned result) or let the call proceed. <br /> |

**See also:**

- [Instrumentation.addMonitor(ActivityMonitor)](https://developer.android.com/reference/android/app/Instrumentation#addMonitor(android.app.Instrumentation.ActivityMonitor))

## Public methods

### getFilter

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

```
public final IntentFilter getFilter ()
```

Retrieve the filter associated with this ActivityMonitor.

<br />

|                                           Returns                                            ||
|--------------------------------------------------------------------------------------|--------|
| [IntentFilter](https://developer.android.com/reference/android/content/IntentFilter) | <br /> |

### getHits

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

```
public final int getHits ()
```

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

<br />

|    Returns    ||
|-------|--------|
| `int` | <br /> |

### getLastActivity

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

```
public final Activity getLastActivity ()
```

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

<br />

|                                     Returns                                      ||
|--------------------------------------------------------------------------|--------|
| [Activity](https://developer.android.com/reference/android/app/Activity) | <br /> |

### getResult

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

```
public final Instrumentation.ActivityResult getResult ()
```

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

<br />

|                                                           Returns                                                            ||
|----------------------------------------------------------------------------------------------------------------------|--------|
| [Instrumentation.ActivityResult](https://developer.android.com/reference/android/app/Instrumentation.ActivityResult) | <br /> |

### isBlocking

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

```
public final boolean isBlocking ()
```

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

<br />

|      Returns      ||
|-----------|--------|
| `boolean` | <br /> |

### onStartActivity

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

```
public Instrumentation.ActivityResult onStartActivity (Intent intent)
```

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[ActivityMonitor()](https://developer.android.com/reference/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.

<br />

|                              Parameters                               ||
|----------|-------------------------------------------------------------|
| `intent` | `Intent`: The intent used for starting the activity. <br /> |

|                                                                                                                                    Returns                                                                                                                                    ||
|----------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Instrumentation.ActivityResult](https://developer.android.com/reference/android/app/Instrumentation.ActivityResult) | The[ActivityResult](https://developer.android.com/reference/android/app/Instrumentation.ActivityResult)that needs to be used in case of a match. <br /> |

### waitForActivity

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

```
public final Activity waitForActivity ()
```

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

<br />

|                                          Returns                                          ||
|--------------------------------------------------------------------------|-----------------|
| [Activity](https://developer.android.com/reference/android/app/Activity) | Activity <br /> |

### waitForActivityWithTimeout

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

```
public final Activity waitForActivityWithTimeout (long timeOut)
```

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.

<br />

|                                       Parameters                                       ||
|-----------|-----------------------------------------------------------------------------|
| `timeOut` | `long`: Time to wait in milliseconds before the activity is created. <br /> |

|                                          Returns                                          ||
|--------------------------------------------------------------------------|-----------------|
| [Activity](https://developer.android.com/reference/android/app/Activity) | Activity <br /> |