FragmentStrictMode.Policy.Builder


public final class FragmentStrictMode.Policy.Builder


Creates 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

Public methods

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

Allow the specified Fragment class to bypass penalties for the specified Violation, if detected.

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

Allow the specified Fragment class to bypass penalties for the specified Violation, if detected.

final @NonNull FragmentStrictMode.Policy

Construct the Policy instance.

final @NonNull FragmentStrictMode.Policy.Builder

Detects cases, where a Fragment instance is reused, after it was previously removed from a FragmentManager.

final @NonNull FragmentStrictMode.Policy.Builder

Detects usage of the tag inside XML layouts.

final @NonNull FragmentStrictMode.Policy.Builder

Detects calls to Fragment.setRetainInstance and Fragment.getRetainInstance.

final @NonNull FragmentStrictMode.Policy.Builder

Detects calls to Fragment.setUserVisibleHint.

final @NonNull FragmentStrictMode.Policy.Builder

Detects calls to Fragment.setTargetFragment, Fragment.getTargetFragment and Fragment.getTargetRequestCode.

final @NonNull FragmentStrictMode.Policy.Builder

Detects cases where a Fragment is added to a container other than a androidx.fragment.app.FragmentContainerView.

final @NonNull FragmentStrictMode.Policy.Builder

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

final @NonNull FragmentStrictMode.Policy.Builder

Throws an exception on violation.

final @NonNull FragmentStrictMode.Policy.Builder

Call OnViolationListener.onViolation for every violation.

final @NonNull FragmentStrictMode.Policy.Builder

Log detected violations to the system log.

Public constructors

Builder

Added in 1.4.0
public Builder()

Public methods

allowViolation

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

Allow the specified Fragment class to bypass penalties for the specified Violation, if detected.

By default, all Fragment classes will incur penalties for any detected Violation.

allowViolation

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

Allow the specified Fragment class to bypass penalties for the specified Violation, if detected.

Since this overload of allowViolation 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 Fragment classes will incur penalties for any detected Violation.

build

Added in 1.4.0
public final @NonNull FragmentStrictMode.Policy build()

Construct the Policy instance.

Note: if no penalties are enabled before calling build, penaltyLog is implicitly set.

detectFragmentReuse

Added in 1.4.0
public final @NonNull FragmentStrictMode.Policy.Builder detectFragmentReuse()

Detects cases, where a Fragment instance is reused, after it was previously removed from a FragmentManager.

detectFragmentTagUsage

Added in 1.4.0
public final @NonNull FragmentStrictMode.Policy.Builder detectFragmentTagUsage()

Detects usage of the tag inside XML layouts.

detectRetainInstanceUsage

Added in 1.4.0
public final @NonNull FragmentStrictMode.Policy.Builder detectRetainInstanceUsage()

Detects calls to Fragment.setRetainInstance and Fragment.getRetainInstance.

detectSetUserVisibleHint

Added in 1.4.0
public final @NonNull FragmentStrictMode.Policy.Builder detectSetUserVisibleHint()

Detects calls to Fragment.setUserVisibleHint.

detectWrongFragmentContainer

Added in 1.4.0
public final @NonNull FragmentStrictMode.Policy.Builder detectWrongFragmentContainer()

Detects cases where a Fragment is added to a container other than a androidx.fragment.app.FragmentContainerView.

detectWrongNestedHierarchy

Added in 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
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
public final @NonNull FragmentStrictMode.Policy.Builder penaltyListener(
    @NonNull FragmentStrictMode.OnViolationListener listener
)

Call OnViolationListener.onViolation for every violation. The listener will be called on the main thread of the fragment host.

penaltyLog

Added in 1.4.0
public final @NonNull FragmentStrictMode.Policy.Builder penaltyLog()

Log detected violations to the system log.