Here's a little sample skeleton for using classes vs activities and making your app more compatible with b4i )
It's more flexible (i.e. create multiple "pages" of the same form, a good example is spreadsheet pages), works better with a drawer and more open to multi-platform, at least in b4x. Of course this is not an exhaustive tutorial, but it should be good enough to get you thinking and started. Perhaps one day I'll add a complete demo app but for now it'll have to sit on the back burner with dozens of other items to do.
TFormClass:
And here is our Activity/Main code:
It's more flexible (i.e. create multiple "pages" of the same form, a good example is spreadsheet pages), works better with a drawer and more open to multi-platform, at least in b4x. Of course this is not an exhaustive tutorial, but it should be good enough to get you thinking and started. Perhaps one day I'll add a complete demo app but for now it'll have to sit on the back burner with dozens of other items to do.
TFormClass:
B4X:
'Class module
Sub Class_Globals
#if b4i
#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")
'seen to need this to make the form/panel opaque
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 b4i
#END IF
End If
'process anything else we want to do
End Sub
#if b4i
sub Form_Appear
'process anything else we want to do
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
B4X:
'Activity/Main
#IF b4a
Sub Globals
Dim ActiveForm As Object 'to hold our current visible form
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
#ELSE IF b4i
Dim ActiveForm As Object 'to hold our current visible form
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 b4i
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
Last edited: