Android Question How to test if object variable is an Activity?

qsrtech

Active Member
Licensed User
Longtime User
I thought

B4X:
if obj is activity then
else
end if
but it doesn't work. Any other way to test if "obj" is an activity?

EDIT:

For now I've resorted to using a try/catch/finally until a better way is established
 
Last edited:

qsrtech

Active Member
Licensed User
Longtime User
Sorry, I can not see the benefits.

Let's see if I understood.

You have the Activity Main, in which you declare x number of classes, we could call them "Activity classes".

Then, at any given time, you use that class and show its layout within a panel.

The class is generic, it does not contain specific code, so all the app code is contained in the Main Activity.

Is it so?
No the class is not generic. You could almost copy and paste your activity to a class. Just need to add some different methods to replace the "activity" ones, well i guess you could almost keep those too if you wanted to open your "class" with them but i opt to using a "visible" property. In the initialize method of the class you pass an event name (if you wanna call back events to the activity), a panel from the main activity to hold your page and the activity it's self. In the classes initialize method you create a panel, load your layout into it, add this panel to the holding panel from the main activity. Call visible from the main activity and bingo. I'll post some code very soon.

Btw i was in Italy/europe a couple months back for my honeymoon ;)
 
Upvote 0

qsrtech

Active Member
Licensed User
Longtime User
Here's a little sample skeleton for using classes vs activities (including some multi-platform ;))

As discussed earlier it's more flexible, works better with a drawer and more open to multi-platform, at least in b4x
TFormClass
B4X:
'Class module
Sub Class_Globals
    #if b4i
    dim parent as Page
    #else if b4a
    Dim Parent As Panel
    Dim Owner As Object
    Dim Event As String
    #End IF
  
    Public Title As String="Your Title"
    #if b4i
    Public Form As Page 'this is the form to hold your layout
    #else if b4a
    Public Form As Panel 'this is the form to hold your layout
    #End IF
    Public Visible As Boolean 'just a place holder
    'views
    Private Button1 As Button 'within layout file
End Sub

'Initializes the object. You can add parameters to this method if needed.
#IF b4a
Public Sub Initialize(AEvent As String, AParent As Panel, ATargetModule As Object) 'add more if you want/need to
    Event=AEvent
    Parent=AParent
    Owner=ATargetModule
    Form.Initialize("Form")
    Parent.AddView(Form,0,0,Parent.Width,Parent.Height)
    Form.LoadLayout("ALayout")
    'seem to need this to make the form/panel opaque after loading a layout file
    Dim cd As ColorDrawable
    cd.Initialize2(Colors.ARGB(255,0,0,0),0,0,0)
    Form.Background=cd
    Form.Visible=False
#ELSE IF #b4i 'this is just a quick example of multi platform
Public Sub Initialize 'add more if you want/need to
    Form.Initialize("Form")
    Form.RootPanel.LoadLayout("ALayout")  
    form.pagetitle=title
#END IF
    'do any other initialization required
End Sub

Sub getVisible As Boolean
    Form.Visible
End Sub

Sub setVisible(value As Boolean)
    Form.Visible=value
    If value Then
        #IF b4a
        Form.BringToFront 'just in case
        #ELSE IF b41
        #END IF
    End If
    'process anything else we want to do
End Sub

#if b41
sub Form_Appear
end sub
#end if

Sub getTitle As String
    Return Title
End Sub

'any view events or other methods for this particular "activity"
Sub BUtton1_Click
    'do something
End Sub

And here is our Activity/Main code
B4X:
'Activity/Main
#IF b4a
Sub Globals
    Dim FormClass As TFormClass
    Dim Page As Panel 'propably in the main layout, otherwise intialize and add to activity
        'if using a drawer then assign the drawer's content panel to this one
End Sub
#END IF

Sub Process_Globals
    Dim Pages As List 'could use a list to hold our forms if we want to
    Pages.Initialize
    Dim ActiveForm As Object 'to hold our current visible form
#ELSE IF b41
    Dim FormClass As TFormClass
#END IF
End Sub

#if b4a
Sub Activity_Create(FirstTime As Boolean)
    'not totally sure yet if we can do this within FirstTime
    Activity.LoadLayout("Layout")
    pages.clear
    FormClass.Initialize("FormClass",Page,Me) 
#ELSE IF b41
Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    pages.clear 'probably not needed
#END IF
    FormClass.Initialize
    pages.add(formclass)
End Sub

'call this when you want to show a particular form
Sub ShowPage(value As Object)
#IF b4a
    'could create a dublicate b4i Navigation class to handle all our page related stuff
    Try'in case our activeform is empty
        'hide any visible one
        CallSub2(ActiveForm,"setVisible",False)
    Catch
    End Try
    ActiveForm=value
    'if we have a toolbar or action bar then
    'toolbar.title=CallSub(ActiveForm,"getTitle")'we use callsub since we are using a generic object type
    CallSub2(ActiveForm,"setVisible",True)
#ELSE IF b4i
    ActiveForm=value
    NavControl.ShowPage(ActiveForm)
#END IF
End Sub
 
Upvote 0

qsrtech

Active Member
Licensed User
Longtime User
I think that Google is changing it's mind moving from multiple Activities, which was the 'standard' some time ago, to one Activitie or at least less Activities in favor of Panels or other specialised views.
That's probably as a result of drawers.

Btw, how far are you away from Mt. Pilatus? My wife and I were sledding there a couple months ago :) We lucked out and had a beautiful clear day (the day before was raining and ugly).
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
In Switzerland, Luzern, which is at the foot of Mt. Pilatus, has the reputation of a rainy region. So I am not surprised that you experienced rainy days.
I live about 250 road km away from Mt. Pilatus in the upper Rhone valley. On the northern foot of the Great St. Bernard pass in a much more sunny area.
 
Upvote 0
Top