FragmentStrictMode.Policy.Builder

public static final class FragmentStrictMode.Policy.Builder
extends Object

java.lang.Object
   ↳ androidx.fragment.app.strictmode.FragmentStrictMode.Policy.Builder


Creates 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

Builder()

Create a Builder that detects nothing and has no violations.

Public methods

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

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

FragmentStrictMode.Policy build()

Construct the Policy instance.

FragmentStrictMode.Policy.Builder detectFragmentReuse()

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

FragmentStrictMode.Policy.Builder detectFragmentTagUsage()

Detects usage of the <fragment> tag inside XML layouts.

FragmentStrictMode.Policy.Builder detectRetainInstanceUsage()

Detects calls to #Fragment.setRetainInstance(boolean) and #Fragment.getRetainInstance().

FragmentStrictMode.Policy.Builder detectSetUserVisibleHint()

Detects calls to #Fragment.setUserVisibleHint(boolean).

FragmentStrictMode.Policy.Builder detectTargetFragmentUsage()

Detects calls to #Fragment.setTargetFragment(Fragment, int), #Fragment.getTargetFragment() and #Fragment.getTargetRequestCode().

FragmentStrictMode.Policy.Builder detectWrongFragmentContainer()

Detects cases where a #Fragment is added to a container other than a #FragmentContainerView.

FragmentStrictMode.Policy.Builder penaltyDeath()

Throws an exception on violation.

FragmentStrictMode.Policy.Builder penaltyListener(FragmentStrictMode.OnViolationListener listener)

Call #FragmentStrictMode.OnViolationListener.onViolation(Violation) for every violation.

FragmentStrictMode.Policy.Builder penaltyLog()

Log detected violations to the system log.

Inherited methods

Public constructors

Builder

public Builder ()

Create a Builder that detects nothing and has no violations.

Public methods

allowViolation

public FragmentStrictMode.Policy.Builder allowViolation (Class<? extends Fragment> fragmentClass, 
                Class<? extends 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.

Parameters
fragmentClass Class

violationClass Class

Returns
FragmentStrictMode.Policy.Builder

build

public FragmentStrictMode.Policy build ()

Construct the Policy instance.

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

Returns
FragmentStrictMode.Policy

detectFragmentReuse

public FragmentStrictMode.Policy.Builder detectFragmentReuse ()

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

Returns
FragmentStrictMode.Policy.Builder

detectFragmentTagUsage

public FragmentStrictMode.Policy.Builder detectFragmentTagUsage ()

Detects usage of the <fragment> tag inside XML layouts.

Returns
FragmentStrictMode.Policy.Builder

detectRetainInstanceUsage

public FragmentStrictMode.Policy.Builder detectRetainInstanceUsage ()

Detects calls to #Fragment.setRetainInstance(boolean) and #Fragment.getRetainInstance().

Returns
FragmentStrictMode.Policy.Builder

detectSetUserVisibleHint

public FragmentStrictMode.Policy.Builder detectSetUserVisibleHint ()

Detects calls to #Fragment.setUserVisibleHint(boolean).

Returns
FragmentStrictMode.Policy.Builder

detectWrongFragmentContainer

public FragmentStrictMode.Policy.Builder detectWrongFragmentContainer ()

Detects cases where a #Fragment is added to a container other than a #FragmentContainerView.

Returns
FragmentStrictMode.Policy.Builder

penaltyDeath

public 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.

Returns
FragmentStrictMode.Policy.Builder

penaltyListener

public FragmentStrictMode.Policy.Builder penaltyListener (FragmentStrictMode.OnViolationListener listener)

Call #FragmentStrictMode.OnViolationListener.onViolation(Violation) for every violation. The listener will be called on the main thread of the fragment host.

Parameters
listener FragmentStrictMode.OnViolationListener

Returns
FragmentStrictMode.Policy.Builder

penaltyLog

public FragmentStrictMode.Policy.Builder penaltyLog ()

Log detected violations to the system log.

Returns
FragmentStrictMode.Policy.Builder