# 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 />

```
public final class FragmentStrictMode.Policy.Builder
```

<br />

*** ** * ** ***

Creates `https://developer.android.com/reference/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/androidx/fragment/app/strictmode/FragmentStrictMode.Policy.Builder#Builder()()` |

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

```
public Builder()
```

## Public methods

### allowViolation

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

```
public final @NonNull FragmentStrictMode.Policy.Builder allowViolation(
    @NonNull Class<@NonNull Fragment> fragmentClass,
    @NonNull Class<@NonNull Violation> violationClass
)
```

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

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

### allowViolation

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

```
public final @NonNull FragmentStrictMode.Policy.Builder allowViolation(
    @NonNull String fragmentClass,
    @NonNull Class<@NonNull Violation> violationClass
)
```

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

Since this overload of `https://developer.android.com/reference/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/androidx/fragment/app/Fragment` classes will incur penalties for any detected `https://developer.android.com/reference/androidx/fragment/app/strictmode/Violation`.

### build

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

```
public final @NonNull FragmentStrictMode.Policy build()
```

Construct the Policy instance.

Note: if no penalties are enabled before calling `build`, `https://developer.android.com/reference/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)

```
public final @NonNull FragmentStrictMode.Policy.Builder detectFragmentReuse()
```

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

### detectFragmentTagUsage

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

```
public final @NonNull FragmentStrictMode.Policy.Builder detectFragmentTagUsage()
```

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)

```
public final @NonNull FragmentStrictMode.Policy.Builder detectRetainInstanceUsage()
```

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

### detectSetUserVisibleHint

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

```
public final @NonNull FragmentStrictMode.Policy.Builder detectSetUserVisibleHint()
```

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

### detectTargetFragmentUsage

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

```
public final @NonNull FragmentStrictMode.Policy.Builder detectTargetFragmentUsage()
```

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

### detectWrongFragmentContainer

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

```
public final @NonNull FragmentStrictMode.Policy.Builder detectWrongFragmentContainer()
```

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

### detectWrongNestedHierarchy

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

```
public final @NonNull FragmentStrictMode.Policy.Builder detectWrongNestedHierarchy()
```

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)

```
public final @NonNull FragmentStrictMode.Policy.Builder penaltyDeath()
```

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)

```
public final @NonNull FragmentStrictMode.Policy.Builder penaltyListener(
    @NonNull FragmentStrictMode.OnViolationListener listener
)
```

Call `https://developer.android.com/reference/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)

```
public final @NonNull FragmentStrictMode.Policy.Builder penaltyLog()
```

Log detected violations to the system log.