Refresh fragment no longer works

Today I lost a few hours because my functional code no longer worked. The code to reload the fragment view no longer worked after upgrading 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 tried debugging setting 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 included in any of these functions, and nothing happened on the screen.

If I call the disconnect alone, without an application, the application enters onPause and onStop, and the view leaves the screen.

+5
android android-fragments fragment android-support-library
Dec 21 '16 at 19:51
source share
3 answers

I ran into the same problem and could not find the answer on the Internet. Finally, I found out that some optimizations were added to Fragment Transactions in version 25.1.1 of the support library. (see https://developer.android.com/topic/libraries/support-library/revisions.html#25-1-1 ). Disabling those who work for your transaction will make it work properly:

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

Update

setAllowOptimization deprecated. Use setReorderingAllowed .

+5
Feb 14 '17 at 9:59
source share

Thanks so much for this. Here is a small modification I made to use getSupportFragmentManager

 FragmentTransaction t = getActivity().getSupportFragmentManager().beginTransaction(); t.setAllowOptimization(false); t.detach(this).attach(this).commitAllowingStateLoss(); 
+1
Apr 17 '17 at 21:25
source share

@Viad actually answered the question. To add a little to this, this happens in versions of Android 26 and higher, where reordering is enabled by default. Reordering comes into play when two fragment operations are requested at the same execution, for example, adding fragment 1 and then replacing it with fragment 2, which leads to the fact that only the latter will happen (replacing fragment 2).

Therefore, when reordering is enabled by default, when you restart a fragment using detach(fragment).attach(fragment) first is ignored and only the second is executed. Since the fragment is already attached, attach(fragment) does nothing. This is why none of the fragment life cycle methods are called.

The solution would be setReorderingAllowed(false) to deactivate reordering. So the solution would be:

  FragmentTransaction transaction = mActivity.getFragmentManager() .beginTransaction(); if (Build.VERSION.SDK_INT >= 26) { transaction.setReorderingAllowed(false); } transaction.detach(fragment).attach (fragment).commit(); 
0
Jul 13 '18 at 14:28
source share



All Articles