Actually, I always reused my view in my fragments like the following:
private View mView = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
if (mView == null)
mView = inflater.inflate(R.layout.view);
return mView;
}
That worked, with viewpager and so on. Now I started using my fragments in simple activities as well and if, and only if, I add the fragment to the backstack, this will fail because of java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
So my questions are:
- Is it ok, if I check the the views parent, remove it and add it to the new parent?
- Or should I always recreate the view and never reuse it? If yes, why?
- Are there other points, where reusing the view will fail?