YoKeyword/Fragmentation · error · RuntimeException
Fragment has not been attached to Activity!
Error message
Fragment has not been attached to Activity!
What it means
getFragmentAnimator() resolves the fragment's animation settings, which needs access to the ISupportActivity bound during onAttach(). If mSupport is null (fragment not attached to a support activity yet), a RuntimeException is thrown.
Solutions
- Call getFragmentAnimator() only after the fragment is attached, e.g. in onActivityCreated() or later.
- Ensure the host activity implements ISupportActivity so mSupport is populated in onAttach().
Example fix
// before
@Override
public void onCreate(Bundle savedInstanceState) {
FragmentAnimator a = getFragmentAnimator();
// after
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
FragmentAnimator a = getFragmentAnimator(); Defensive patterns
Strategy: validation
Validate before calling
if (isAdded()) { FragmentAnimator a = getFragmentAnimator(); } Try / catch
try {
FragmentAnimator a = getFragmentAnimator();
} catch (RuntimeException e) {
// defer animator access to a later lifecycle callback
} Prevention
- Query the animator in onActivityCreated() or later.
- Never call delegate methods before onAttach().
- Check isAdded() before touching activity-bound APIs.
When it happens
Trigger: Calling getFragmentAnimator() from the fragment's constructor or before onAttach; onCreate() ordering issues where the animator is requested prior to attach; hosting fragment in a non-ISupportActivity activity so mSupport never set.
Common situations: Reading the animator in onCreate before super.onCreate finishes attach wiring; deserialized/retained fragments queried too early; testing fragments without an activity.
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
- not attach!
- Must extends Fragment
- There is no Fragment in the FragmentManager, maybe you need…
- Can't find container, please call loadRootFragment() first!
- Must extends FragmentActivity/AppCompatActivity
AI-assisted analysis of YoKeyword/Fragmentation@0394930a3e (2026-09-10).
Data as JSON: /api/errors/53e91a2b135ac9a1.
Report an issue: GitHub.
Appendix: source
Thrown at fragmentation_core/src/main/java/me/yokeyword/fragmentation/SupportFragmentDelegate.java:317
return getVisibleDelegate().isSupportVisible();
}
/**
* Set fragment animation with a higher priority than the ISupportActivity
* 设定当前Fragmemt动画,优先级比在ISupportActivity里高
*/
public FragmentAnimator onCreateFragmentAnimator() {
return mSupport.getFragmentAnimator();
}
/**
* 获取设置的全局动画
*
* @return FragmentAnimator
*/
public FragmentAnimator getFragmentAnimator() {
if (mSupport == null)
throw new RuntimeException("Fragment has not been attached to Activity!");
if (mFragmentAnimator == null) {
mFragmentAnimator = mSupportF.onCreateFragmentAnimator();
if (mFragmentAnimator == null) {
mFragmentAnimator = mSupport.getFragmentAnimator();
}
}
return mFragmentAnimator;
}
/**
* Set the fragment animation.
*/
public void setFragmentAnimator(FragmentAnimator fragmentAnimator) {
this.mFragmentAnimator = fragmentAnimator;
if (mAnimHelper != null) {
mAnimHelper.notifyChanged(fragmentAnimator);
}View on GitHub (pinned to 0394930a3e)