Android Question does initializing a panel remove its child views?

Dave O

Well-Known Member
Licensed User
Longtime User
I have a panel that contains other views.

Normally, if I were to get rid of the panel (e.g. with RemoveView), I would first remove all of its child views using RemoveAllViews.

But what happens if I just leave the panel there (for later reuse), and later initialize it again (and re-add it to the activity)? What happens to its former child views? Are they effectively removed from it as if I had done a RemoveAllViews? Or do they get stuck in limbo?

The latter is what my current code does, and I'm OK with leaving it that way if I'm not leaving zombie views lying around.
 

klaus

Expert
Licensed User
Longtime User
Panel1.RemoveView removes the Panel with all its child views!
If you only reinitialize and re-add the Panel you generate a new instance of the Panel which means a new Panel. The previous Panel with all its views remains.
If you do this 10 times you will have 11 Panels !
If you want to keep the Panel but remove its child views you must use Panel1.RemoveAllViews.
 
Upvote 0

Dave O

Well-Known Member
Licensed User
Longtime User
Thanks @klaus .

Two more follow-up questions, just so I'm clear:

> Panel1.RemoveView removes the Panel with all its child views!

Is this the same as doing panel1.RemoveAllViews followed by panel1.RemoveView? If so, it's a nice shortcut.

> If you only reinitialize and re-add the Panel you generate a new instance of the Panel which means a new Panel. The previous Panel with all its views remains.

Is this also true for class instances in general, not just views? In other words, if I re-init an object, does the old object still exist with all its contents, or does it get garbage-collected at some point?

Thanks again!
 
Last edited:
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Panel1.RemoveView removes the Panel with all its child views!

Is this the same as doing panel1.RemoveAllViews followed by panel1.RemoveView? If so, it's a nice shortcut.

It's not exactly the same thing from the GC point of view but in practice that doesn't make a real difference.

Is this also true for class instances in general, not just views? In other words, if I re-init an object, does the old object still exist with all its contents, or does it get garbage-collected at some point?
It depends on what is behind Initialize. So it depends on the developer who wrote the class/library. Creating an instance is done by Dim, so Initialize should not be used to create a new instance. But the fact is that Initialize is often used to create an underlying instance in a wrapper. For example, the ArrayList object, which is the Java object behind a list, is created when you call List.Initialize.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…