Do Samsung devices handle FragmentTransactions differently than other devices?

I was trying to solve a problem that is driving me crazy. I have a single action app that shows different screens by sharing dynamic fragments, i.e. the usual

getFragmentManager().beginTransaction().replace(R.id.fragementContainer, new SomeFragment()).commit();

      

This works great on my Nexus 4, Nexus 5, Nexus 7 2012, Nexus 7 2013, Moto G 1st gen, etc. Basically everything except Samsung devices (SGS5, SGS6). On these devices, sometimes (rarely, but not uncommon) a call to replace () seems to be misinterpreted as a call to add () , and a new fragment appears on top of the previous one.

I tried to replicate the error but there doesn't seem to be any pattern for it. Since I don't get any error messages anywhere, I can't seem to fix them.

+3


source to share


2 answers


This is why I always use support libraries. The source code is in your project and has not been modified by third parties. Already using a support package?



+3


source


This may be due to some Samsung optimizations. My only advice is to use remove()

and add()

instead replace()

. The implementation FragmentTransaction

is different internally , so there is a chance it will behave differently.

You can easily delete a fragment because it has the same ID as the container. Try the following:



FragmentTransaction transaction = fragmentManager.beginTransaction();

Fragment oldFragment = fragmentManager.findFragmentById(R.id.container);
if (oldFragment != null) transaction.remove(oldFragment);

transaction.add(R.id.container, new Fragment(), "tag").commit();

      

+1


source







All Articles