{"record":{"id":"53e91a2b135ac9a1","repo":"YoKeyword/Fragmentation","slug":"fragment-has-not-been-attached-to-activity","errorCode":null,"errorMessage":"Fragment has not been attached to Activity!","messagePattern":"Fragment has not been attached to Activity!","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"fragmentation_core/src/main/java/me/yokeyword/fragmentation/SupportFragmentDelegate.java","lineNumber":317,"sourceCode":"        return getVisibleDelegate().isSupportVisible();\n    }\n\n    /**\n     * Set fragment animation with a higher priority than the ISupportActivity\n     * 设定当前Fragmemt动画,优先级比在ISupportActivity里高\n     */\n    public FragmentAnimator onCreateFragmentAnimator() {\n        return mSupport.getFragmentAnimator();\n    }\n\n    /**\n     * 获取设置的全局动画\n     *\n     * @return FragmentAnimator\n     */\n    public FragmentAnimator getFragmentAnimator() {\n        if (mSupport == null)\n            throw new RuntimeException(\"Fragment has not been attached to Activity!\");\n\n        if (mFragmentAnimator == null) {\n            mFragmentAnimator = mSupportF.onCreateFragmentAnimator();\n            if (mFragmentAnimator == null) {\n                mFragmentAnimator = mSupport.getFragmentAnimator();\n            }\n        }\n        return mFragmentAnimator;\n    }\n\n    /**\n     * Set the fragment animation.\n     */\n    public void setFragmentAnimator(FragmentAnimator fragmentAnimator) {\n        this.mFragmentAnimator = fragmentAnimator;\n        if (mAnimHelper != null) {\n            mAnimHelper.notifyChanged(fragmentAnimator);\n        }","sourceCodeStart":299,"sourceCodeEnd":335,"githubUrl":"https://github.com/YoKeyword/Fragmentation/blob/0394930a3e2368f210df31f2632fb89b9c44e121/fragmentation_core/src/main/java/me/yokeyword/fragmentation/SupportFragmentDelegate.java#L299-L335","documentation":"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.","triggerScenarios":"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.","commonSituations":"Reading the animator in onCreate before super.onCreate finishes attach wiring; deserialized/retained fragments queried too early; testing fragments without an activity.","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()."],"exampleFix":"// before\n@Override\npublic void onCreate(Bundle savedInstanceState) {\n    FragmentAnimator a = getFragmentAnimator();\n// after\n@Override\npublic void onActivityCreated(@Nullable Bundle savedInstanceState) {\n    FragmentAnimator a = getFragmentAnimator();","handlingStrategy":"validation","validationCode":"if (isAdded()) { FragmentAnimator a = getFragmentAnimator(); }","typeGuard":null,"tryCatchPattern":"try {\n    FragmentAnimator a = getFragmentAnimator();\n} catch (RuntimeException e) {\n    // defer animator access to a later lifecycle callback\n}","preventionTips":["Query the animator in onActivityCreated() or later.","Never call delegate methods before onAttach().","Check isAdded() before touching activity-bound APIs."],"tags":["android","fragment","lifecycle"],"backgroundTag":"invalid-state-transition","analyzedSha":"0394930a3e2368f210df31f2632fb89b9c44e121","analyzedAt":"2026-09-10T13:16:30.469Z","contentChangedAt":"2026-09-10T13:16:30.469Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}