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.
not attached to a contextnot attached to an activityFragment not attached to a context 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
- Launch work in viewLifecycleOwner.lifecycleScope
- Check isAdded before using context
- Cancel callbacks in onDestroyView
Detailed fix by platform
Kotlin
- kotlin
viewLifecycleOwner.lifecycleScope.launch { val data = repo.load() if (isAdded) binding.title.text = getString(R.string.loaded, data.name) }
How to diagnose
- Callback — Which async call?
- 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:
Similar errors
- 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
- ActivityNotFoundException ActivityNotFoundException: Unable to find explicit activity class; have you declared this activity in your AndroidManifest.xml?
- adb unauthorized adb: device unauthorized. This adb server's $ADB_VENDOR_KEYS is not set
Most viewed in Android
- Could not resolve Gradle: Could not resolve all files for configuration / Could not resolve dependency
- Unsupported class file major version Gradle: Unsupported class file major version / Android Gradle plugin requires Java 17 to run
- INSTALL_PARSE_FAILED_NO_CERTIFICATES INSTALL_PARSE_FAILED_NO_CERTIFICATES / App not installed as package appears to be invalid
Other ways to find it
Where this error sits
Origin: On your device. Errors that are often confused with or caused by this one (click to open):
🧠 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.
Report a correction or suggest an improvement
Last updated 12 Oct 2026