Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment already added. Support lib

Since I upgraded my App to use support lib rev. 11 and compiled against SDK 4.2 I receive the following error

java.lang.IllegalStateException: Fragment already added: i{4079e598 #2 id=0x1020002 i}
    at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1159)
    at android.support.v4.app.BackStackRecord.popFromBackStack(BackStackRecord.java:729)
    at android.support.v4.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1493)
    at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:477)
    at android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:174)

I have read about similar problems on stackoverflow. Some of them suggest to avoid the replace method, which I am not using. The transaction that causes the crash on rollback looks like this:

ft.remove(oldFrag).add(newFrag).addToBackStack(null).commit()

Other comments say this may occur if you are using fragment transitions that can't finish quick enough before the user presses back again. Since I do use transition animations on the given fragments, this might be the cause. However, removing the transition is not a choice and since this exclusively occurs on phones running 2.3.x and the bug was introduced by upgrading to lib 11 and sdk 4.2 I hope google will fix it with the next release.

Has anyone similar problems, information about whether this is planned to be fixed in 4.2.1 or support r12, or suggestions what I can do to circumvent the error?

like image 458
phlebas Avatar asked Dec 06 '12 14:12

phlebas


2 Answers

I am trying to fix the issue using a hint I found here

So I added executePendingTransactions() to immediately execute the transaction:

ft.remove(oldFrag).add(newFrag).addToBackStack(null).commit()
getFragmentManager().executePendingTransactions();

This should ensure that the backstack is always in a clean state I hope and doesn't have any negative drawbacks I can think of.

Update Although it does work on regular phones, the issue remains on some generic devices that do not have a known Android Version number, like 4.0.1 or similar, but instead report some Date like 'Thu Mar 02 2006 00:00:00 GMT+0100 (MEZ) ' as Android Version Number, e.g: samsung - espressowifixx - GT-P3110 reports the given date as android version and yields the fragment added exception... Still annoying. Users don't care about this stuff, so it is always the already deficitary developer who is blamed. No problem for billion dollar companies of course, but single developers, just do something more useful with your time than write apps ;)

like image 139
phlebas Avatar answered Nov 06 '22 20:11

phlebas


So for me, it turned out that I was actually trying to add a fragment twice. Maybe in previous support library versions it just swallowed this and moved on. Unfortunately, even checking if the fragment isAdded() didn't work for me, because in my case, I was calling them back to back before the transaction had time to actually add the first fragment.

What I ended up doing was creating a boolean class variable fragmentAdded and setting it to true immediately after I made the transaction. Then I check this boolean before trying to add the fragment. May not be a solution to your problem, but for me it was, so hopefully it helps.

like image 40
Jason Robinson Avatar answered Nov 06 '22 21:11

Jason Robinson