FragmentStrictMode.Policy.Builder


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 functions

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

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

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

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

FragmentStrictMode.Policy

Construct the Policy instance.

FragmentStrictMode.Policy.Builder

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

FragmentStrictMode.Policy.Builder

Detects usage of the tag inside XML layouts.

FragmentStrictMode.Policy.Builder

Detects calls to Fragment.setRetainInstance and Fragment.getRetainInstance.

FragmentStrictMode.Policy.Builder

Detects calls to Fragment.setUserVisibleHint.

FragmentStrictMode.Policy.Builder

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

FragmentStrictMode.Policy.Builder

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

FragmentStrictMode.Policy.Builder

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

FragmentStrictMode.Policy.Builder

Throws an exception on violation.

FragmentStrictMode.Policy.Builder

Call OnViolationListener.onViolation for every violation.

FragmentStrictMode.Policy.Builder

Log detected violations to the system log.

Public constructors

Builder

Added in 1.4.0
Builder()

Public functions

allowViolation

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

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
fun allowViolation(fragmentClass: String, violationClass: Class<Violation>): FragmentStrictMode.Policy.Builder

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
fun build(): FragmentStrictMode.Policy

Construct the Policy instance.

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

detectFragmentReuse

Added in 1.4.0
fun detectFragmentReuse(): FragmentStrictMode.Policy.Builder

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

detectFragmentTagUsage

Added in 1.4.0
fun detectFragmentTagUsage(): FragmentStrictMode.Policy.Builder

Detects usage of the tag inside XML layouts.

detectSetUserVisibleHint

Added in 1.4.0
fun detectSetUserVisibleHint(): FragmentStrictMode.Policy.Builder

Detects calls to Fragment.setUserVisibleHint.

detectWrongFragmentContainer

Added in 1.4.0
fun detectWrongFragmentContainer(): FragmentStrictMode.Policy.Builder

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

detectWrongNestedHierarchy

Added in 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
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
fun penaltyListener(listener: FragmentStrictMode.OnViolationListener): FragmentStrictMode.Policy.Builder

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
fun penaltyLog(): FragmentStrictMode.Policy.Builder

Log detected violations to the system log.

Discover the latest app development tools, platform updates, training, and documentation for developers across every Android device.

Updated Apr 11, 2024

Request location permission in Android 1

Updated Sep 21, 2023

Discover the latest app development tools, platform updates, training, and documentation for developers across every Android device.

Updated Sep 19, 2024