Change theme
Help
Press space for more information.
Show links for this issue (Shortcut: i, l)
Copy issue ID
Previous Issue (Shortcut: k)
Next Issue (Shortcut: j)
Sign in to use full features.
Vote: I am impacted
Notification menu
Refresh (Shortcut: Shift+r)
Go home (Shortcut: u)
Pending code changes (auto-populated)
View issue level access limits(Press Alt + Right arrow for more information)
Unintended behavior
View staffing
Description
1. Create fragment objects A and B
2. Programmatically add fragment A to an activity
3. Replace fragment A with fragment B (using OP_REPLACE)
4. Pop the transaction from step 3 from the back stack
5. Repeat step 3
Symptom:
An IllegalStateException is thrown with the message "Fragment already added:...".
Cause:
The proximate cause of the problem is that fragment B's mImmediateActivity member is non-null. This is because in BackStackRecord.popFromBackStack, in BackStackRecord.java line 610, the code says "f.mImmediateActivity = mManager.mActivity;" rather than what I believe to be the correct code "old.mImmediateActivity = mManager.mActivity;" This appears to be a copy-and-paste bug caused by combining the OP_ADD and OP_REMOVE cases without realizing that the variable "f" refers to two different fragments in the two cases.
Workaround:
Instead of using OP_REPLACE, the calling code can remove fragment A and add fragment B in two separate operations. This avoids the erroneous code.