Android Question How to determine if Object is Activity or View?

Widget

Well-Known Member
Licensed User
Longtime User
If I have a Sub with a parameter aObject as Object, how can I determine if the aObject that was passed is Activity or a View?

The only way to determine it is to use a Try/Catch block which isn't very clean.
Is there a better way?

B4X:
public Sub GetViewDim(aObject As Object) As TRectDim
    Private Result As TRectDim
    If aObject Is View Then
        Try
            Private locView As View
            locView = aObject
            Result.Initialize(locView.Left, locView.Top, locView.Width, locView.Height)
        Catch
            Log(LastException)
            Private locActivity As Activity
            locActivity = aObject
            Result.Initialize(locActivity.Left, locActivity.Top, locActivity.Width, locActivity.Height)
        End Try
    Else 
        If aObject Is CustomListView Then
            Private locCLV As CustomListView
            locCLV = aObject
            Result.Initialize(locCLV.Left, locCLV.Top, locCLV.Width, locCLV.Height)
        Else
            RaiseException("[GetViewDim] Unknown View "&GetType(aObject), False, True)
        End If
    End If
    Return Result
End Sub

I thought I could use:
B4X:
if aObject is Activity then
  private locActivity as Activity
  locActivity = aObject
  Result.Initialize(locActivity.Left, locActivity.Top, locActivity.Width, locActivity.Height)
else if aObject is View then
            Private locView As View
            locView = aObject
            Result.Initialize(locView.Left, locView.Top, locView.Width, locView.Height)
       end if
end if

But all views (panel, button etc.) that are passed will execute:
"if aObject is Activity then"

It appears all Views Is Activity and Activity is View. This does not make sense to me because if I try and reference Activity as View it throws an exception:
"java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to anywheresoftware.b4a.BALayout$LayoutParams"

So is there a better way to detect between a View and Activity?
TIA
 

Widget

Well-Known Member
Licensed User
Longtime User
Found it on forum
B4X:
Sub DisplayClass(Obj As Activity)
    Try
        Obj.GetStartingIntent
        Log("this is an activity")
    Catch
        Log("this is a panel")
    End Try
End Sub

Yes that does work, thanks. But it is trading one exception for another.
I was hoping I could avoid exceptions so I came up with this:

B4X:
'Experimental Beta Version
Sub IsActivity(aView As View) As Boolean
    Private Result As Boolean=False
    If aView.IsInitialized Then
        If aView.Parent <> Null Then
            If GetType(aView.Parent).Contains("Frame") Then
                Result = True
            End If
        End If
    End If
    Return Result
End Sub

The parent of an Activity is "android.widget.FrameLayout" whereas the parent of a View is "anywheresoftware.b4a.BALayout".
This seems to work, but I will have to test it some more.
 
Upvote 0

Widget

Well-Known Member
Licensed User
Longtime User
This will not work in all cases.

What do you need it for?

I wrote a small sub GetViewDim() (see above) that has an Object parameter and the sub returns its dimension. It is a trivial Sub() and occasionally I pass it the Activity so I can get its dimension returned in my TRectDim class (similar to Rect).

I didn't think it would be this hard to determine if the object passed is a View or Activity. I thought I overlooked something trivial. (I guess not. o_O)

But if it does pose a problem I will simply add a 2nd parameter "IsActivity Boolean" like:
public Sub GetViewDim(aObject As Object, IsActivity as Boolean) As TRectDim

and when I pass it Activity, I will set the 2nd parameter True.
TIA
 
Upvote 0
Top