Android Question [B4X] Is this the new correct way to determine if a view/B4xview has been added?

MrKim

Well-Known Member
Licensed User
Longtime User
For a standard view you check for .Parent = null but that doesn't work with B4x Views but this Seems to work but also seems a little onerous:
B4X:
        Dim VV As B4XView = xui.CreatePanel("")
        Log(VV.Parent.As(String).EqualsIgnoreCase("(B4XView) Not initialized"))
'OR
        Dim Txt As Label
        Txt.Initialize("")
        Log(Txt.As(B4XView).Parent.As(String).EqualsIgnoreCase("(B4XView) Not initialized"))
It is nice to have only one way to do this.
As a side note I noticed that you can AS a b4xview as a B4Xview
B4X:
Dim VV As B4XView = xui.CreatePanel("")
Log(VV.As(B4XView).Parent.As(String).EqualsIgnoreCase("(B4XView) Not initialized"))
Which means (If I am correct in my assumptions) the following function would be the universal way to test any view to see it is is loaded:
B4X:
Sub IsLoaded(BXV As B4XView) As Boolean
    Return Not(BXV.As(B4XView).Parent.As(String).EqualsIgnoreCase("(B4XView) Not initialized"))
End Sub
AND If this is valid I would add a WISH that this be added as a Core function (perhaps XUI.IsLoaded(V As View)?).
 

LucaMs

Expert
Licensed User
Longtime User
1703921895822.png


So

If vv.Parent.IsIntialized = False Then

(but I think "If vv..IsIntialized = False Then" too)


[ - 12 šŸ˜„ ]
 
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
View attachment 149085

So

If vv.Parent.IsIntialized = False Then

(but I think "If vv..IsIntialized = False Then" too)


[ - 12 šŸ˜„ ]
That's the problem, I think you will find that a B4XView if it has been initialize WILL return True, even if it has no parent. But I did change my routine to:
B4X:
Sub IsLoaded(BXV As B4XView) As Boolean
    If Not(BXV.IsInitialized) Then Return False
    Return Not(BXV.Parent.As(String).EqualsIgnoreCase("(B4XView) Not initialized"))
End Sub
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
That's the problem, I think you will find that a B4XView if it has been initialize WILL return True, even if it has no parent.
Indeed, maybe because a B4XView is a "layer" over the actual native view(s).
What does a view need to exist?
1 to be declared, at what stage it is not initialization, nor it has a parent.
2 to be initialization, but may still not have a parent.
3 to be added to a parent. Note that the parent is also a view, with exception to main activity, root pane, etc.
Also not that having a parent doesn't mean the view is visible.

So, how to determine if a view "exists"?
If the the view is initiated, and it has a parent ans the parent is initiated... I would say it exists
 
Upvote 0
Top