Android ViewStub管理替换内部视图

Android ViewStub管理替换内部视图,android,android-inflate,viewstub,Android,Android Inflate,Viewstub,我正在使用ViewStubAndroid组件进行测试。我可以将视图充气并替换为新视图,但当我将现有视图替换为新视图时,我怀疑在旧的替换视图中从未调用销毁程序。下面是我的逻辑示例代码: protected void testInflateReplaceViewStub(){ //case inflating a View... View viewInflated = View.inflate(getContext(), R.layout.container_view, this);

我正在使用
ViewStub
Android组件进行测试。我可以将
视图充气并替换为新视图,但当我将现有
视图替换为新视图时,我怀疑在旧的替换视图中从未调用销毁程序。下面是我的逻辑示例代码:

protected void testInflateReplaceViewStub(){
    //case inflating a View...
    View viewInflated = View.inflate(getContext(), R.layout.container_view, this); //container View
    ViewStub viewStub = (ViewStub) findViewById(R.id.view_stub_impl); //view inside layout
    viewStub.setLayoutResource(R.layout.container_view); //fill with some content
    View viewStubInflated = viewStub.inflate();
    //end inflating the first ViewStub

    //case replacing above View by a new one...
    viewStubInflated.invalidate(); //this should destroy old inner Views??
    viewInflated = View.inflate(getContext(), R.layout.container_view, this);
    viewStub.setLayoutResource(R.layout.new_content);//fill new content
    viewStubInflated = viewStub.inflate();
    //end replacing inflating
}

我想强制销毁所有已替换的旧
视图

一个将
视图存根充气的视图
它将从层次结构中消失,并且不能再次使用:

布局资源“mySubTree”膨胀后,ViewStub 从其父级移除

根据您的需要,尝试使用
include
甚至
FrameLayout