gyf-dev/ImmersionBar · error · IllegalArgumentException

Fragment must be attached to an Activity.

Error message

Fragment must be attached to an Activity.

What it means

The package-private ImmersionBar(Fragment) constructor calls fragment.getActivity() and requires a live Activity reference, because ImmersionBar ultimately applies insets to the Activity's Window. If getActivity() returns null the fragment is not attached, and the constructor throws IllegalArgumentException.

Solutions

  1. Call ImmersionBar.with(fragment).init() only after the fragment is attached (onViewCreated or later)
  2. Guard with fragment.isAdded() / fragment.getActivity() != null before using ImmersionBar
  3. Cancel pending bar setup in onDetach and re-apply in onResume

Example fix

// before
public void onCreate(Bundle s) { ImmersionBar.with(this).init(); } // may throw
// after
@Override
public void onViewCreated(View v, Bundle s) {
    if (isAdded()) {
        ImmersionBar.with(this).statusBarDarkFont(true).init();
    }
}
Defensive patterns

Strategy: validation

Validate before calling

if (fragment.isAdded() && fragment.getActivity() != null) { ImmersionBar.with(fragment).init(); }

Type guard

boolean fragmentReady(Fragment f) { return f != null && f.isAdded() && f.getActivity() != null; }

Try / catch

try { ImmersionBar.with(fragment).init(); } catch (IllegalArgumentException e) { Log.w(TAG, "Fragment not attached, skipping bar init"); }

Prevention

When it happens

Trigger: Calling ImmersionBar.with(fragment) inside Fragment constructor, onCreate before onAttach completes (or after onDetach), or from a fragment whose host Activity is being destroyed.

Common situations: Initializing the bar in a field initializer; calling init() from an async callback that returns after detach; using the fragment in a detached state (e.g. retained fragment, notification-triggered work).

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of gyf-dev/ImmersionBar@8cac10cd83 (2026-09-08). Data as JSON: /api/errors/893ed8f69d9cc28d. Report an issue: GitHub.

Appendix: source

Thrown at immersionbar/src/main/java/com/gyf/immersionbar/ImmersionBar.java:3069

     *
     * @param activity the activity
     */
    ImmersionBar(Activity activity) {
        mActivity = activity;
        initCommonParameter(mActivity.getWindow());
    }

    /**
     * 在Fragment里初始化
     * Instantiates a new Immersion bar.
     *
     * @param fragment the fragment
     */
    ImmersionBar(Fragment fragment) {
        mIsFragment = true;
        Activity activity = fragment.getActivity();
        if (activity == null) {
            throw new IllegalArgumentException("Fragment must be attached to an Activity.");
        }
        mActivity = activity;
        mSupportFragment = fragment;
        checkInitWithActivity();
        initCommonParameter(mActivity.getWindow());
    }

    /**
     * 在Fragment里初始化
     * Instantiates a new Immersion bar.
     *
     * @param fragment the fragment
     */
    ImmersionBar(android.app.Fragment fragment) {
        mIsFragment = true;
        mActivity = fragment.getActivity();
        mFragment = fragment;
        checkInitWithActivity();

View on GitHub (pinned to 8cac10cd83)