I need to update a fragment with a different views.
As I figured out, to force Fragment::onCreateView() method call (to show my updates to the user) I can only use FragmentTransaction’s method replace(, ).
It works good if I create a new Fragment and a new view for returning it from onCreateView() method every time I call FragmentTransaction::replace().
But I wish to keep some views in memory.
How can I reuse my views?
The problem is that if I use my view (that was attached to already replaced fragment) at a new fragment, I get an exception:
“java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child’s parent first.”
So, I try to detach the view from fragment by calling FragmentTransaction.detach() or FragmentTransaction.remove() to cause removing fragment from the activity and my view from already replaced fragment, but it doesn’t help.
Does anybody know how to force a fragment to remove its child view (that was returned inside onCreateView()?
You’re better off keeping the view contained within the Fragment so the Fragment itself handles it appropriately. Either have viewless Fragments send data to that Fragment to update the View or build the View with the parameters. It’s cleaner and in the long run, much more maintainable.
Check out the Adding a Fragment without UI section.