How to pass an Activity object to a code module?

tamadon

Active Member
Licensed User
Longtime User
I am trying to move my animation code into a code module as they've been used repeatedly in several activities.

Can I pass the Activity instance into a code module? I am trying the following code

B4X:
' In code module
Sub EnterButtonAnim(Anim As AnimationPlus, Act As Activity)
   For i = 0 To Act.NumberOfViews - 1
      Dim currentButton As Button      
      
      If Act.GetView(i) Is Button Then
         currentButton = Act.GetView(i)   
         
         AnimButtonRight(i+1, Anim)
         If Anim.IsInitialized Then
            Anim.Stop(currentButton)
            Act.Invalidate
            Anim.Start(currentButton)
         End If
         
      End If
   Next
End Sub

Then I try calling the function from the Activity module using

B4X:
catmodule.EnterButtonAnim(AnimButton, Me)

It doesn't work of course and I get

B4X:
Compiling code.                         0.03
Compiling layouts code.                 0.00
Generating R file.                      0.31
Compiling generated Java code.          Error
B4A line: 171
catmodule.EnterButtonAnim(AnimButton, Me)
javac 1.6.0_26
src\com\dbcom\findthecat\main.java:455: inconvertible types
found   : java.lang.Class<capture#652 of ?>
required: anywheresoftware.b4a.BALayout
mostCurrent._catmodule._enterbuttonanim(mostCurrent.activityBA,mostCurrent._animbutton,(anywheresoftware.b4a.objects.ActivityWrapper) anywheresoftware.b4a.AbsObjectWrapper.ConvertToWrapper(new anywheresoftware.b4a.objects.ActivityWrapper(), (anywheresoftware.b4a.BALayout)(main.getObject())));

Any suggestion? Thanks!
 

galimpic

Member
Licensed User
Longtime User
Use Object in function declaration, then cast it with a variable:

B4X:
Sub EnterButtonAnim(Anim As AnimationPlus, Act1 As Object)
Dim Act As Activity = Act1
...
 
Upvote 0

tamadon

Active Member
Licensed User
Longtime User
Thanks galimpic. I tried your suggestion but get

B4X:
java.lang.ClassCastException: java.lang.Class

on
B4X:
Dim Act As Activity = Act1
 
Upvote 0

galimpic

Member
Licensed User
Longtime User
OK, this one works :)

instead of
B4X:
catmodule.EnterButtonAnim(AnimButton, Me)
use
B4X:
catmodule.EnterButtonAnim(AnimButton, Activity)
 
Upvote 0
Top