【发布时间】:2023-04-02 17:26:01
【问题描述】:
实际上,我总是在片段中重复使用我的视图,如下所示:
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;
}
使用 viewpager 等有效。现在我也开始在简单的活动中使用我的片段,当且仅当我将片段添加到后台堆栈时,这将失败,因为java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
所以我的问题是:
- 可以吗,如果我检查视图父级,将其删除并添加到新父级?
- 或者我应该总是重新创建视图并且从不重复使用它?如果是,为什么?
- 是否还有其他点会导致重用视图失败?
【问题讨论】:
标签: android view android-fragments reusability