Fragment not attached to a context Android Origin: On your device Last updated 12 Oct 2026

IllegalStateException: Fragment x not attached to a context / not attached to an activity

Code called requireContext()/requireActivity()/getString() after the Fragment was detached — usually in an async callback.

Exact message you may see not attached to a contextnot attached to an activityFragment not attached to a context
Seen on: Android

Meaning

Network or database callbacks finishing after the user navigated away run against a detached fragment. Collect data with viewLifecycleOwner and check isAdded.

Common causes

  • Async callback after onDetach/onDestroyView
  • Using requireContext in a coroutine not tied to the view lifecycle
  • Retained listeners

⚡ Quick fix

  1. Launch work in viewLifecycleOwner.lifecycleScope
  2. Check isAdded before using context
  3. Cancel callbacks in onDestroyView

Detailed fix by platform

Kotlin

  1. kotlin
    viewLifecycleOwner.lifecycleScope.launch {
        val data = repo.load()
        if (isAdded) binding.title.text = getString(R.string.loaded, data.name)
    }

How to diagnose

  1. Callback — Which async call?
  2. Scope — lifecycleScope vs viewLifecycleOwner

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: On your device. Errors that are often confused with or caused by this one (click to open):

A Ticker was active when the State was disposed — Flutter: _HomeState#abc(ticker active) was disposed with an active Ticker. State.dispose() implementations must call dispose() on all their AnimationControllers (Flutter / Dart)A Ticker was active w…ActivityNotFoundException — ActivityNotFoundException: Unable to find explicit activity class; have you declared this activity in your AndroidManifest.xml? (Android)ActivityNotFoundExcep…adb unauthorized — adb: device unauthorized. This adb server's $ADB_VENDOR_KEYS is not set (Android)adb unauthorizedAndroid resource linking failed — AAPT: error: resource x (aka y) not found — Android resource linking failed (Android)Android resource link…android:attr/lStar not found — AAPT: error: resource android:attr/lStar not found (Android)android:attr/lStar no…BadTokenException — WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? (Android)BadTokenExceptionCan not perform this action after onSaveInstanceState — IllegalStateException: Can not perform this action after onSaveInstanceState (fragment transaction) (Android)Can not perform this …dependOnInheritedWidgetOfExactType called before initState completed — Flutter: dependOnInheritedWidgetOfExactType<_InheritedTheme>() or dependOnInheritedElement() was called before _HomeState.initState() completed (Flutter / Dart)dependOnInheritedWidg…Fragment …

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.