{"record":{"id":"b41973eb49e3d7cd","repo":"DrKLO/Telegram","slug":"layoutmanager-layout-is-already-attached-to-a-re","errorCode":null,"errorMessage":"LayoutManager {layout} is already attached to a RecyclerView:{recyclerView}","messagePattern":"LayoutManager (.+?) is already attached to a RecyclerView:(.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"TMessagesProj/src/main/java/androidx/recyclerview/widget/RecyclerView.java","lineNumber":1397,"sourceCode":"            }\n            mLayout.removeAndRecycleAllViews(mRecycler);\n            mLayout.removeAndRecycleScrapInt(mRecycler);\n            mRecycler.clear();\n\n            if (mIsAttached) {\n                mLayout.dispatchDetachedFromWindow(this, mRecycler);\n            }\n            mLayout.setRecyclerView(null);\n            mLayout = null;\n        } else {\n            mRecycler.clear();\n        }\n        // this is just a defensive measure for faulty item animators.\n        mChildHelper.removeAllViewsUnfiltered();\n        mLayout = layout;\n        if (layout != null) {\n            if (layout.mRecyclerView != null) {\n                throw new IllegalArgumentException(\"LayoutManager \" + layout\n                        + \" is already attached to a RecyclerView:\"\n                        + layout.mRecyclerView.exceptionLabel());\n            }\n            mLayout.setRecyclerView(this);\n            if (mIsAttached) {\n                mLayout.dispatchAttachedToWindow(this);\n            }\n        }\n        mRecycler.updateViewCacheSize();\n        requestLayout();\n    }\n\n    /**\n     * Set a {@link OnFlingListener} for this {@link RecyclerView}.\n     * <p>\n     * If the {@link OnFlingListener} is set then it will receive\n     * calls to {@link #fling(int,int)} and will be able to intercept them.\n     *","sourceCodeStart":1379,"sourceCodeEnd":1415,"githubUrl":"https://github.com/DrKLO/Telegram/blob/45ab8f4308496e1f01026a97fcdb0d58a5274474/TMessagesProj/src/main/java/androidx/recyclerview/widget/RecyclerView.java#L1379-L1415","documentation":"setLayoutManager checks layout.mRecyclerView before binding. A LayoutManager may be attached to at most ONE RecyclerView; if it is already bound, attaching it again throws 'LayoutManager is already attached to a RecyclerView'. A LayoutManager holds per-RecyclerView state (recycler, view, animations) that cannot be shared.","triggerScenarios":"Creating one LinearLayoutManager instance and calling recyclerViewA.setLayoutManager(lm) then recyclerViewB.setLayoutManager(lm). Reusing a LayoutManager field across recreated fragments/activities without nulling it.","commonSituations":"Singleton or shared LayoutManager injected into multiple RecyclerViews. Fragment view recreation where the LM is retained in a ViewModel and re-applied to a new RecyclerView instance.","solutions":["Create a new LayoutManager instance per RecyclerView; do not share instances.","If reusing programmatically, first call oldRecyclerView.setLayoutManager(null) to detach before binding to a new one.","Store LayoutManager creation in onViewCreated so each view gets its own instance."],"exampleFix":"// before (shared field)\nprivate final LinearLayoutManager lm = new LinearLayoutManager(ctx);\nrecyclerViewA.setLayoutManager(lm);\nrecyclerViewB.setLayoutManager(lm); // throws\n\n// after\nrecyclerViewA.setLayoutManager(new LinearLayoutManager(ctx));\nrecyclerViewB.setLayoutManager(new LinearLayoutManager(ctx));","handlingStrategy":"validation","validationCode":"if (lm != null && lm.getRecyclerView() != null) {\n    // already attached elsewhere; create a fresh instance instead\n    lm = new LinearLayoutManager(ctx);\n}\nrecyclerView.setLayoutManager(lm);","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Create a new LayoutManager instance per RecyclerView.","If migrating an LM to a new RecyclerView, call setLayoutManager(null) on the old one first.","Do not store a LayoutManager in a shared singleton/ViewModel for reuse across views."],"tags":["recyclerview","layoutmanager","lifecycle","shared-state"],"backgroundTag":null,"analyzedSha":"45ab8f4308496e1f01026a97fcdb0d58a5274474","analyzedAt":"2026-08-14T05:19:30.815Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}