Fragmenttransaction Replace Not Working
Solution 1:
Please take into account that fragmentTransaction.replace
is (at the time of writing this) buggy - see https://code.google.com/p/android/issues/detail?id=24674.
What you could do now is
- to star the issue (at the link provided) and
- come up with a workaround until Google has it fixed.
My workaround is to manually remove all relevant fragments (with the help of fragmentManager.getFragments()
) and to add the new fragment normally with fragmentTransaction.add
.
Solution 2:
It looks like you have an issue with OldFragment. In your second code block, you have slideMenuActivity.getSupportFragmentManager().findFragmentById(R.id.dashboard)
which implies that oldFragment = R.id.dashboard, and that it is added in xml. This method has two problems:
- You can't replace fragments that are added in xml, you can only replace ones that were added dynamically.
- You have to call replace on the container that has the fragment, not the fragment itself. So instead of calling
replace(R.id.dashboard, fragment)
it should bereplace(R.id.dashboards_container, fragment)
.
Also, if you are toggling between the same fragments, consider using attach and detach instead of replace. Attach and detach only destroy the view hierarchy, while replace destroys them completely. Thus, attach and detach will increase performance if you are trying to reuse the same fragments. See my answer here for an example of attach/detach.
Solution 3:
It looks like the first block of code is correct. Can you add the code where you dynamically add the first fragment?
If it just so happens you do that in your onCreateView(), it could be adding it twice. The first one will be correctly replaced, but an older copy that was semi-destroyed when you navigated away and came back by way of the Receiver, will still be shown.
Post a Comment for "Fragmenttransaction Replace Not Working"