I have a main activity which contains a viewPager and a tabLayout. On the tabLayout I set following code :
@Override
public void onTabReselected(TabLayout.Tab tab) {
setToolbarIcon(0);
setToolbarMode(0);
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.view_container,pageFragment[tab.getPosition()]);
fragmentTransaction.commit();
}
with pageFragment = new Fragment[5]{new fragmentA(),new fragmentB(),new fragmentC(), new fragmentD(), new fragmentE()};
So, I get the error saying java.lang.IllegalStateException: Can't change container ID of fragment FragmentNewsfeed{c897339 #0 id=0x7f0d0083 android:switcher:2131558531:0}: was 2131558531 now 2131558601
but what's weird is if I change fragmentTransaction.replace(R.id.view_container,pageFragment[tab.getPosition()]);
into fragmentTransaction.replace(R.id.view_container,new someFragment());
it's working fine, and after that if I revert it back to the first-version of code it's also working fine. What drives me crazy is sometimes the error shows up and sometime the error disappear. I've tried to remove the fragment before replacing but it doesn't work.
So what should I do? Why does the problem sometimes appear but sometimes don't?
note : R.id.view_container is the id I put for the parent FrameLayout in every of my Fragment