Search code examples
androidandroid-fragmentsfragmentandroid-support-library

Refresh fragment is not working any more?


I lost some hours today because my code was not working any more. The code to reload the view of a fragment was not working anymore after updating to the new version of Support Library 25.1.0:

This is my code :

FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
fragmentTransaction.detach(fragment);
fragmentTransaction.attach(fragment);
fragmentTransaction.commit();

I have tried to debug putting some breakpoints on

public void onPause()
public void onStop()
public void onAttach(Context context)
public void onDetach()  
public void onDestroyView()
public void onDestroy()

but the application is not entering into any of that function and nothing happened on the screen.

If I call detach alone, without attach, the application enter in onPause and onStop and the view leave the screen.


Solution

  • I've found myself facing the same issue, and found no answer online. Finally I've found out that some optimizations were added to Fragment Transactions in Revision 25.1.1 of the Support Library. (see https://developer.android.com/topic/libraries/support-library/revisions.html#25-1-1). Disabling those for your transaction will make it work as expected:

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.setAllowOptimization(false);
    transaction.detach(fragment).attach(fragment).commitAllowingStateLoss();
    

    Update

    setAllowOptimization is deprecated. Use setReorderingAllowed instead.