Android Question callsub and polymorphism

Discussion in 'Android Questions' started by advansis, Sep 15, 2018.

  1. advansis

    advansis Member Licensed 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...:(

    Code:
    ' 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
     
  2. OliverA

    OliverA Expert Licensed User

    Maybe this?
    Also
    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
     
    LucaMs likes this.
  3. LucaMs

    LucaMs Expert Licensed User

    Just use CallSubDelayed3 and the Activity will start.
     
  4. advansis

    advansis Member Licensed User

    Thank you LucaMs, but I need to pre-load some Activity values before loading it, so I should use CallSub...
    I.E:
    Code:
    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...
     
  5. LucaMs

    LucaMs Expert Licensed User

    The difference between CallSubDelayed and CallSub is that the first one starts the target Activity:
    Also, in your last code you're using StartActivity, then I think you did not want to write: "so i should use CallSub...".
     
  6. advansis

    advansis Member Licensed 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.
     
  7. OliverA

    OliverA Expert Licensed User

    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.
     
    LucaMs likes this.
  8. advansis

    advansis Member Licensed 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...
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice