Android Question Call sub from class

demasi

Active Member
Licensed User
Longtime User
Hello,

I'm trying to write a class, but I need to call a sub in the activity where I'm using this class.
I tried with callsub, but it doesn't worked.
Is there a way to do this?

eg:

I have a main activity and a class. in this class I need to call a sub at the main activity.
 

demasi

Active Member
Licensed User
Longtime User
Thank you for your reply.
My code only worked when I used CallSubDelayed, no way with CallSub, and I think the result is a something strange.

This is my main activity:

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim test As MyClass
    test.Initialize(Me,"Message")
    Log("Sum=" & test.sum(4,5))
End Sub

Sub Message
    Log("Hello!")
End Sub

And this is my class code:

B4X:
'Class module
Sub Class_Globals
    Dim wMod As Object
    Dim wSub As String
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(xMod As Object, xSub As String)
    wMod = xMod
    wSub = xSub  
End Sub

Sub sum(a As Int, b As Int)
    CallSub(wMod,wSub)
    Log("1")

    CallSub(wMod,wSub)
    Log("2")

    CallSubDelayed(wMod,wSub)
    Log("3")

    CallSubDelayed(wMod,wSub)
    Log("4")

    Return a+b
End Sub

This code resulted this when running:

upload_2015-9-10_12-6-42.png


Why CallSub didn´t returned anything?
Why the sequence of the results are different from the code?

I think the results should be:

B4X:
Hello!
1
Hello!
2
Hello!
3
Hello!
4
Sum=9

I have a class where I create a list and need to select some itens. I want to leave the Activity process each click on this list.
What is the correct way to make this?

Thank you!
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Log("Sum=" & test.sum(4,5))
At this point your activity just runs through activity_create but it is still NOT VISIBLE... If you try to use callsub from your class it will not work; callsub can just call a sub from a visible activity.

Log("Sum=" & test.sum(4,5))
Put this code into a button click in your activity...
 
Upvote 0

demasi

Active Member
Licensed User
Longtime User
When I used the code in the button it worked as expected.
I´ve noted that in my example above, IsPaused(WMod) returns true.
When I use the button, it returns false.
Without the button, callsub does not work. with the button it works, and CallSubDelayed works at the end of all.
I studied the Android Life Cycle, but this is still confusing to me.
I will keep trying to understand this.
Thank you.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
but this is still confusing to me.
I will keep trying to understand this.
All code in activity create will run when the activity is not visible.
Your old code does not fire the sub (callsub) because the activity is not visible.
Callsubdelay will put the eventcall into a messaging queue till the activity is visible. That´s the reason why your two HEllo will raise after the activity resumed.

Using a button your code can use callsub because the activity is already visible.
 
Upvote 0

demasi

Active Member
Licensed User
Longtime User
Thank you DonManfred.

Another thing: All the variables I define in my class, no matter I use DIM, PUBLIC or PRIVATE, shows in the list when I use an instance of this class.
I studied other classes code, but I coudn´t realize how they hide this variables.
I need to expose only some variables, other are private. How can I do this?
 
Upvote 0

demasi

Active Member
Licensed User
Longtime User
Yes, you´re right.
But it worked only after I saved, closed, and opened again the project.
By the way, I love your libs.
Thank you!
 
Upvote 0
Top