If 'ft' is not null, I think it should pass 'ft' and not to call getActiveTransaction(). With the current code, two transactions are oppened, because 'onTabUnselected' and 'onTabSelected' are called on two different TabWrapper instances, therefore mActiveTransaction is null for the second one, when 'onTabSelected' is called, and the first one gets never commited.
I've attached a test project showing the bug. On prev ICS devices, only one fragment is show at time, on ICS devices (event an emulator) the old tabs gets never hidden.
3 解决办法:
I've written this util class to by pass the bug for now. Just put it after the last line of your 'onTabUnselected' method:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
try {
Method commit = tab.getClass().getDeclaredMethod("commitActiveTransaction");
commit.setAccessible(true);
commit.invoke(tab);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
1 Bug:There is a bug on ActionBarImplICS.java, line 454:https://android.googlesource.com/platform/frameworks/support/+/master/v7/appcompat/src/android/support/v7/app/ActionBarImplICS.java2 解释:I