Billyjoker
Billyjoker

Reputation: 751

Android ViewStub manage replace inner Views

i am testing with the ViewStub Android component. I am able to inflate a View and replace by a new one, but when i perform a replace of the existing View by a new one, i suspect that the destroyer is never called at the old replaced Views. Here is an example code of my logic:

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
}

I'd like to force destroying all the old replaced Views.

Upvotes: 0

Views: 1232

Answers (1)

miguel_rossi
miguel_rossi

Reputation: 53

One you inflate the ViewStub it disappears of the hierarchy and you cannot use it again:

After inflation of the layout resource "mySubTree," the ViewStub is removed from its parent.

See ViewStub documentation.

Try using include or even FrameLayout, depend on your needs.

Upvotes: 1

Related Questions