Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4android > Basic4android Getting started & Tutorials
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Basic4android Getting started & Tutorials Android development starts here. Please do not post questions in this sub-forum.

Static Code Modules

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-27-2011, 03:35 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default Static Code Modules

Basic4android v1.2 includes a new type of module which is the static code module.
Adding a static code module is done by choosing Project - Add New Module - Code Module.

Unlike Activities and Services, code modules are not related in any way with Android process life cycle. These are just containers for code.
All of the subs in these modules are public and can be accessed from other modules.

Code modules use cases include:
- Avoiding duplicate code in multiple modules.
- Sharing code between projects. For example you can create a code module that parses some file. Later you can easily reuse this module in other applications.
- Separating your application logic. Each code module can be used for a specific task. This will make your program clearer and easier to maintain.

As a code module is not tied to an activity or service it uses the calling component context when required.
For example, calling a code module sub that shows a Msgbox from an activity will work. However if you call it from a service it will fail as services are not allowed to show dialogs.

Code modules cannot catch events.
While you can use a code module to initialize a button for example:
Code:
Sub ButtonsCreator(Text As StringAs Button
  
Dim b As Button
  b.Initialize(
"Button")
  b.Text = Text
  
Return b
End Sub
From the activity module you can call:
Code:
Activity.AddView(CodeModule.ButtonsCreator("press here"), 10dip10dip200dip200dip)
Now in order to catch the click event you should create a sub named Button_Click.
This sub should be located in the Activity module, as Code modules cannot catch events.
CallSub which internally uses the events mechanism cannot be used to call code module subs (which can be called directly instead).
Reply With Quote
  #2 (permalink)  
Old 06-13-2011, 11:11 PM
nfordbscndrd's Avatar
Basic4ppc Expert
 
Join Date: Jan 2011
Location: Hot Springs Village, AR, USA
Posts: 792
Default

I'm trying to create sample code for static modules, but it's not working as expected:

Code:
Main:
Sub Process_Globals
   
Dim i As Int
End Sub
 
Sub Activity_Create
   i = 
1
   Mod1.Do1
End sub
 
Mod1:
Sub Do1
   
Dim j As Int
   j = i + 
1
End Sub
Your post says that subs "can be called directly" and "All of the subs in these modules are public and can be accessed from other modules."

I consider "called directly" to simply be "Do1" (as in VB6) rather than "Mod1.Do1".

In the IDE under Sub Process_Globals, it says: "These variables can be accessed from all modules", which I took to mean that the variables AND THEIR VALUES could be shared among modules. Yet when I run the code above, I get the error that in Sub Do1, "Undeclared variable 'i' is used before it was assigned any value." Am I doing something wrong, or is it just that the values can NOT be shared?
Reply With Quote
  #3 (permalink)  
Old 06-14-2011, 06:23 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

The values can be shared but you need to prefix the variable with the module name. There could be several variables named i in different modules.
Code:
Main:
Sub Process_Globals
   
Dim i As Int
End Sub
 
Sub Activity_Create
   i = 
1
   Mod1.Do1
End sub
 
Mod1:
Sub Do1
   
Dim j As Int
   j = i + 
1
End Sub
You cannot call a Sub in a different Activity or Service. You will need to use CallSub for that. This is what I meant with direct calling a Sub that belongs to a code module.
Reply With Quote
  #4 (permalink)  
Old 08-13-2011, 02:08 PM
JesseW's Avatar
Basic4ppc Veteran
 
Join Date: Oct 2008
Posts: 250
Default Market apps

I am preparing my first app for market, and I'm considering having a free version that has a 4 second splash, instead of 2 seconds, and an AdMob view at the bottom of the screen.

I didn't find any reference in the forums for the best way to achieve this, so I've created a code module and managed all the code from the activity module to the code module. Like this:

Main: Activity module
Code:
Sub Process_Globals
End Sub

Sub Globals
    
Dim sv As ScrollView
    
Dim lblRecap(157As Label
    
Dim lstRecap As List
    
Dim tmrSplash As Timer
    
Dim pnlSplash As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
    
'The last parameter of the below funcion call should be:
    '  0dip for the paid version
    ' 50dip for the free version to make room for the AdMob view
    Recap.ActivityCreate(FirstTime, Activity, lblRecap, sv, pnlSplash, tmrSplash, 0dip)  'use code module code
End Sub

Sub Activity_Resume
    Recap.ActivityResume(lstRecap, lblRecap)  
'use code module code
End Sub

Sub Activity_KeyPress(KeyCode As IntAs Boolean
    
If keycode = KeyCodes.KEYCODE_BACK Then
        Activity.Finish
        
Return False
    
End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

'The only click event for the 54 display labels
Sub lblRecap_Click
    
Dim lbl As Label
    lbl = 
Sender
    Recap.LabelClick(Activity, lbl, lstRecap, lblRecap)  
'use code module code
End Sub

Sub tmrSplash_tick
    pnlSplash.Visible = 
False
    tmrSplash.Enabled = 
False
End Sub
Recap: code module
Code:
Sub Process_Globals
    ...
    
'dim my global program variables here
    ...
End Sub

Sub ActivityCreate(FirstTime As Boolean, Activity As Activity, lblRecap(,) As Label, sv As ScrollView, pnlSplash As Panel, tmrSplash As Timer, AdMobOffset As Int)
    
If FirstTime Then
        ...
        
' set up splash screen here
        ...
    
End If
    ...
    
'Set up screen display elements
    ...
End Sub

Sub ActivityResume(lstRecap As List, lblRecap(,) As Label)
    ...
    
'load data from file and populate display labels
    ...
End Sub

Sub LabelClick(Activity As Activity, lbl As Label, lstRecap As List, lblRecap(,) As Label)
    ...
    
'input line data from user, save in data file and update display labels
    ' the passed parameter 'lbl' is the sender label
    ...
End Sub

Sub Calc(lstRecap As List, lblRecap(,) As Label)
    ...
    
'recalculate display column data based on data load or user input
    ...
End Sub

Sub Init(lstRecap As List)
    ...
    
'initialize the internal data list with initial zero data
    ...
End Sub

Sub Disclaimer
    ...
    
'setup disclaimer screen
    ...
End Sub
So I've placed all the 'meat' of the project code into a code module, which can be used in two projects. I plan to have one B4A project for the free version, and one for the paid version. The free version will pass '50dip' in Main.Activity_Create to Recap.ActivityCreate as the final parameter, which will move the scrollview up from the bottom 50 dip's to make room for the AdMob view and lengthen the splash screen from 2 to 4 seconds.

So I have a few questions:
1) is this the best way to achieve this 'dual apk' setup? or is there a better method, like conditional compilation that I've overlooked...
2) if yes, should I place both project file in the same folder, or have two different folders?

Thanks anyone / everyone for your help
Jesse
Reply With Quote
  #5 (permalink)  
Old 08-14-2011, 07:28 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

You can use a boolean variable to set the running mode. Though it will cause the full code to be distributed in the trial version.

I don't recommend you to place both projects in the same folder. During compilation the compiler creates all kinds of files. It assumes that there are no other projects in the same folder.
Reply With Quote
  #6 (permalink)  
Old 08-14-2011, 08:23 AM
Basic4ppc Veteran
 
Join Date: Feb 2011
Location: Chicago area (NW Indiana, USA)
Posts: 325
Default

I think I have seen it requested here before, but here is a +1 for conditional compilation (such as in VB6).

I currently use a boolean to determine demo or full version, but also plan to have two copies of the project in order to keep the "prime code" out of the demo. I may try using this code module sub to simplify things and maybe just comment out stuff that doesn't need to be in the demo (and vice versa) so I can keep it as one project and just swap commented code blocks before building. I imagine I'd also need to change the name in the project as well before compiling.

Yeah.... +1 to offer conditional compiling with the ability to conditionally add or remove code blocks and change package names based on the compiling condition chosen.


Last edited by Kevin : 08-14-2011 at 08:27 AM. Reason: Edited my Swype failure. Next best thing in soft keyboards, they said!
Reply With Quote
  #7 (permalink)  
Old 08-15-2011, 02:40 PM
JesseW's Avatar
Basic4ppc Veteran
 
Join Date: Oct 2008
Posts: 250
Default

Quote:
Originally Posted by Erel View Post
You can use a boolean variable to set the running mode. Though it will cause the full code to be distributed in the trial version.

I don't recommend you to place both projects in the same folder. During compilation the compiler creates all kinds of files. It assumes that there are no other projects in the same folder.
Thanks Erel.. After considering all this, I'm not even sure conditional compilation would suit this scenario, because at minimum, the free and full versions would have different package names. I think two different projects in two folders, with both accessing a single code module with any differences being in the project activity code is my best bet.

That said, is there any reason one project cannot use a code module from another project?

(well crap - I see I wasn't supposed to ask questions in this forum - my apologies for starting this discussion here )
Reply With Quote
  #8 (permalink)  
Old 08-15-2011, 04:04 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Quote:
That said, is there any reason one project cannot use a code module from another project?
You cannot reference a module from a different project. You will need to make a copy of the module.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Static Google Maps Pachuquin Questions (Windows Mobile) 1 06-24-2010 05:48 PM
Static Navigation & GPSDriver.dll numerus Questions (Windows Mobile) 8 05-16-2010 11:16 AM
Some Modules klaus Code Samples & Tips 9 03-14-2009 03:25 PM
how to save a static image cotralis Questions (Windows Mobile) 3 02-07-2009 04:05 PM
Set ON/OFF static navigation schimanski Questions (Windows Mobile) 2 12-07-2008 04:44 PM


All times are GMT. The time now is 10:29 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0