I cache the fragments (have very few of them) as they are view intensive and inflate takes time which i am trying to avoid.
So in each fragment class we have static View currentView = null; which is populated once that fragment calls on ViewCreate.
if (currentView == null)
currentView = inflater.inflate(R.layout.fragment_menu, container, false);
Now i am 99% sure this worked before that i could do the following:
Fragment_proxydisplay_menu menu = new Fragment_proxydisplay_menu(); //instantiated and entered some items for the menu
menu.AddItems("1","2");
And eventually called fragment manager to display this created fragment:
FragmentManager fragmentManager = myActivity.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content, menu, menu.getClass().getSimpleName());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commitAllowingStateLoss();
fragmentManager.executePendingTransactions();
Now the IllegalStateException comes into play if one of my items triggers a submenu i do the above again with different items and get
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:5124)
at android.view.ViewGroup.addView(ViewGroup.java:4953)
at android.view.ViewGroup.addView(ViewGroup.java:4893)
at androidx.fragment.app.FragmentStateManager.addViewToContainer(FragmentStateManager.java:840)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:529)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1890)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1808)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1751)
at androidx.fragment.app.FragmentManager.executePendingTransactions(FragmentManager.java:614)
If i show a another lets say "please wait" simple fragment between to menu fragments it works. And I am sure this worked in sdk 25 or less because now is the first time I am getting this.
I tried removing the parent from currentView as per some research:
if(currentView == null)
currentView = inflater.inflate(R.layout.fragment_proxydisplay_menu, container, false);
else
((ViewGroup) currentView.getParent()).removeView(currentView);
Now i do not track which fragment is on screen as they are prettymuch selfcontained so why would this error start to happen in the newer sdk? How can i reuse the inflated fragment and repaint it with different data without knowing that i must not use the same screen in a row.
Thank you
from Fragment IllegalStateException when reusing inflated fragment
No comments:
Post a Comment