# FragmentStrictMode.Policy.Builder

Artifact: [androidx.fragment:fragment](https://developer.android.com/jetpack/androidx/releases/fragment) [View Source](https://cs.android.com/search?q=file:androidx/fragment/app/strictmode/FragmentStrictMode.kt+class:androidx.fragment.app.strictmode.FragmentStrictMode.Policy.Builder) Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/fragment#1.4.0)

*** ** * ** ***

<br />

```
class FragmentStrictMode.Policy.Builder
```

<br />

*** ** * ** ***

Creates `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/FragmentStrictMode.Policy` instances. Methods whose names start with `detect` specify what problems we should look for. Methods whose names start with `penalty` specify what we should do when we detect a problem.

You can call as many `detect` and `penalty` methods as you like. Currently order is insignificant: all penalties apply to all detected problems.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/FragmentStrictMode.Policy.Builder#Builder()()` |

| ### Public functions |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/FragmentStrictMode.Policy.Builder` | `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/FragmentStrictMode.Policy.Builder#allowViolation(java.lang.Class,java.lang.Class)( fragmentClass: https://developer.android.com/reference/java/lang/Class.html<https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment>, violationClass: https://developer.android.com/reference/java/lang/Class.html<https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/Violation> )` Allow the specified `https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment` class to bypass penalties for the specified `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/Violation`, if detected. |
| `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/FragmentStrictMode.Policy.Builder` | `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/FragmentStrictMode.Policy.Builder#allowViolation(kotlin.String,java.lang.Class)(fragmentClass: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html, violationClass: https://developer.android.com/reference/java/lang/Class.html<https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/Violation>)` Allow the specified `https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment` class to bypass penalties for the specified `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/Violation`, if detected. |
| `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/FragmentStrictMode.Policy` | `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/FragmentStrictMode.Policy.Builder#build()()` Construct the Policy instance. |
| `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/FragmentStrictMode.Policy.Builder` | `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/FragmentStrictMode.Policy.Builder#detectFragmentReuse()()` Detects cases, where a `https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment` instance is reused, after it was previously removed from a `https://developer.android.com/reference/kotlin/androidx/fragment/app/FragmentManager`. |
| `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/FragmentStrictMode.Policy.Builder` | `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/FragmentStrictMode.Policy.Builder#detectFragmentTagUsage()()` Detects usage of the tag inside XML layouts. |
| `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/FragmentStrictMode.Policy.Builder` | `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/FragmentStrictMode.Policy.Builder#detectRetainInstanceUsage()()` Detects calls to `https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment#setRetainInstance(boolean)` and `https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment#getRetainInstance()`. |
| `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/FragmentStrictMode.Policy.Builder` | `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/FragmentStrictMode.Policy.Builder#detectSetUserVisibleHint()()` Detects calls to `https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment#setUserVisibleHint(boolean)`. |
| `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/FragmentStrictMode.Policy.Builder` | `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/FragmentStrictMode.Policy.Builder#detectTargetFragmentUsage()()` Detects calls to `https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment#setTargetFragment(androidx.fragment.app.Fragment,int)`, `https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment#getTargetFragment()` and `https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment#getTargetRequestCode()`. |
| `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/FragmentStrictMode.Policy.Builder` | `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/FragmentStrictMode.Policy.Builder#detectWrongFragmentContainer()()` Detects cases where a `https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment` is added to a container other than a `https://developer.android.com/reference/kotlin/androidx/fragment/app/FragmentContainerView`. |
| `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/FragmentStrictMode.Policy.Builder` | `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/FragmentStrictMode.Policy.Builder#detectWrongNestedHierarchy()()` Detects nested fragments that do not use the expected parent's childFragmentManager. |
| `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/FragmentStrictMode.Policy.Builder` | `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/FragmentStrictMode.Policy.Builder#penaltyDeath()()` Throws an exception on violation. |
| `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/FragmentStrictMode.Policy.Builder` | `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/FragmentStrictMode.Policy.Builder#penaltyListener(androidx.fragment.app.strictmode.FragmentStrictMode.OnViolationListener)(listener: https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/FragmentStrictMode.OnViolationListener)` Call `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/FragmentStrictMode.OnViolationListener#onViolation(androidx.fragment.app.strictmode.Violation)` for every violation. |
| `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/FragmentStrictMode.Policy.Builder` | `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/FragmentStrictMode.Policy.Builder#penaltyLog()()` Log detected violations to the system log. |

## Public constructors

### Builder

Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/fragment#1.4.0)

```
Builder()
```

## Public functions

### allowViolation

Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/fragment#1.4.0)

```
fun allowViolation(
    fragmentClass: Class<Fragment>,
    violationClass: Class<Violation>
): FragmentStrictMode.Policy.Builder
```

Allow the specified `https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment` class to bypass penalties for the specified `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/Violation`, if detected.

By default, all `https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment` classes will incur penalties for any detected `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/Violation`.

### allowViolation

Added in [1.5.0](https://developer.android.com/jetpack/androidx/releases/fragment#1.5.0)

```
fun allowViolation(fragmentClass: String, violationClass: Class<Violation>): FragmentStrictMode.Policy.Builder
```

Allow the specified `https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment` class to bypass penalties for the specified `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/Violation`, if detected.

Since this overload of `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/FragmentStrictMode.Policy.Builder#allowViolation(kotlin.String,java.lang.Class)` takes in the name of the Fragment class as a string, rather than accepting the Class itself, the user will need to manually insure the class is not obfuscated.

By default, all `https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment` classes will incur penalties for any detected `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/Violation`.

### build

Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/fragment#1.4.0)

```
fun build(): FragmentStrictMode.Policy
```

Construct the Policy instance.

Note: if no penalties are enabled before calling `build`, `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/FragmentStrictMode.Policy.Builder#penaltyLog()` is implicitly set.

### detectFragmentReuse

Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/fragment#1.4.0)

```
fun detectFragmentReuse(): FragmentStrictMode.Policy.Builder
```

Detects cases, where a `https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment` instance is reused, after it was previously removed from a `https://developer.android.com/reference/kotlin/androidx/fragment/app/FragmentManager`.

### detectFragmentTagUsage

Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/fragment#1.4.0)

```
fun detectFragmentTagUsage(): FragmentStrictMode.Policy.Builder
```

Detects usage of the tag inside XML layouts.

### detectRetainInstanceUsage

Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/fragment#1.4.0)

```
fun detectRetainInstanceUsage(): FragmentStrictMode.Policy.Builder
```

Detects calls to `https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment#setRetainInstance(boolean)` and `https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment#getRetainInstance()`.

### detectSetUserVisibleHint

Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/fragment#1.4.0)

```
fun detectSetUserVisibleHint(): FragmentStrictMode.Policy.Builder
```

Detects calls to `https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment#setUserVisibleHint(boolean)`.

### detectTargetFragmentUsage

Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/fragment#1.4.0)

```
fun detectTargetFragmentUsage(): FragmentStrictMode.Policy.Builder
```

Detects calls to `https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment#setTargetFragment(androidx.fragment.app.Fragment,int)`, `https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment#getTargetFragment()` and `https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment#getTargetRequestCode()`.

### detectWrongFragmentContainer

Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/fragment#1.4.0)

```
fun detectWrongFragmentContainer(): FragmentStrictMode.Policy.Builder
```

Detects cases where a `https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment` is added to a container other than a `https://developer.android.com/reference/kotlin/androidx/fragment/app/FragmentContainerView`.

### detectWrongNestedHierarchy

Added in [1.6.0](https://developer.android.com/jetpack/androidx/releases/fragment#1.6.0)

```
fun detectWrongNestedHierarchy(): FragmentStrictMode.Policy.Builder
```

Detects nested fragments that do not use the expected parent's childFragmentManager.

### penaltyDeath

Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/fragment#1.4.0)

```
fun penaltyDeath(): FragmentStrictMode.Policy.Builder
```

Throws an exception on violation. This penalty runs at the end of all enabled penalties so you'll still get to see logging or other violations before the exception is thrown.

### penaltyListener

Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/fragment#1.4.0)

```
fun penaltyListener(listener: FragmentStrictMode.OnViolationListener): FragmentStrictMode.Policy.Builder
```

Call `https://developer.android.com/reference/kotlin/androidx/fragment/app/strictmode/FragmentStrictMode.OnViolationListener#onViolation(androidx.fragment.app.strictmode.Violation)` for every violation. The listener will be called on the main thread of the fragment host.

### penaltyLog

Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/fragment#1.4.0)

```
fun penaltyLog(): FragmentStrictMode.Policy.Builder
```

Log detected violations to the system log.