Android Question callsub and polymorphism

advansis

Active Member
Licensed User
Longtime User
Hi guys,
I have some Activities that share one or more functions. In this wy I realize a sort of polymorphism.
But, the following code seems not to work...:(

B4X:
' Activity_A
sub Fooo (A as string, B as string)
'...
end sub

' Activity_B
sub Fooo (A as string, B as string)
'...
end sub

' Activity_C
sub Fooo (A as string, B as string)
'...
end sub

' This is a CLASS in wich I will choose the activity to load

Sub Class_Globals
    Dim MyAct as Object
    ' ...
end sub

' ...
sub OpenActivity (ActName as string,ParA as string,ParB as string)
    select case ActName
        case "A"
            MyAct=Activity_A
        case "B"
            MyAct=Activity_B
        case "C"
            MyAct=Activity_C
        case else
            return
    end select
   
    ' These callsub is not fired !!!
    callsub3(MyAct,"Fooo",ParA,ParB)
end sub

Is there any way to obtain what I need ? Thanks in advance
 

OliverA

Expert
Licensed User
Longtime User
Maybe this?
Calls the given sub. CallSub can be used to call a sub which belongs to a different module.
However the sub will only be called if the other module is not paused. In that case an empty string will be returned.
You can use IsPaused to test whether a module is paused.
Also
This means that one activity cannot call a sub of a different activity. As the other activity will be paused for sure.
So if your class module is instantiated in an activity module, then this code will not work.

Source: https://www.b4x.com/android/forum/pages/results/?query=callsub
 
Upvote 0

advansis

Active Member
Licensed User
Longtime User
Thank you LucaMs, but I need to pre-load some Activity values before loading it, so I should use CallSub...
I.E:
B4X:
MyActivity.UseMyToolbar=true
MyActivity.SetUser(....)
' ...
StartActivity(MyActivity)

Instead, with a lot of work, I am creating a single Map for setting all Activity parameters and trying to solve it in such a way. Probably I will have to find something to manage Activity Events in my class...
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Thank you LucaMs, but I need to pre-load some Activity values before loading it, so I should use CallSub...
The difference between CallSubDelayed and CallSub is that the first one starts the target Activity:
But, the following code seems not to work..
' These callsub is not fired !!!
callsub3(MyAct,"Fooo",ParA,ParB)

Also, in your last code you're using StartActivity, then I think you did not want to write: "so i should use CallSub...".
 
Upvote 0

advansis

Active Member
Licensed User
Longtime User
Pardòn, LucaMs,probably I didn't explain correctly...

I have several Activities (5 at the moment) with common lines of code and views (let's say about 60%): THE SAME IDENTICAL CODE, managing activity views/events.
To avoid code replication (and propagation of updates), I created a class that provides common functionalities (of the 60% of code).
I needed a way to call Activity methods from my class, before the activity was shown (Activity_Create).
I cannot use CallSubDelayed (believe me, I know the difference between CallSubX and CallSubDelayedX), because in the Activity_Create() I call a different layout depending on the context. I set this context BEFORE calling StartActivity(), by setting several variables: myActivity.myVar1=myValue1 ... myActivity.myVarX=myValueX
Hope I explained correctly...
Anyway, I am trying to solve my problem by providing a Map Object to the Activity, reducing the code inside each activity to only one line.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
I needed a way to call Activity methods from my class, before the activity was shown (Activity_Create).
Is that even possible? I think whatever you need done before Activity_Create you need to perform in the Starter service. You can then access the information from your activitie(s) through the Starter service.
 
Upvote 0

advansis

Active Member
Licensed User
Longtime User
@OliverA, It is possible.
I access each global var in Activity, BEFORE calling Start_Activity(). I did it several times. Probably because activity object is a singleton...
 
Upvote 0
Top