Android Question how to manage object activity into class?

Roberto P.

Well-Known Member
Licensed User
Longtime User
I ask for help to understand how to manage the need for a dialogue between a module with a class activity.
I mean, I've created a module-type activity to manage the interface and a class to implement its management (business logic).
I can not "pass" the object activity within the class to handle its members. as in C + +, I would like to pass a pointer \ reference class in generic class activity.
I hope I have explained clearly.
thanks
 

DonManfred

Expert
Licensed User
Longtime User
B4X:
Private Sub Class_Globals
  Private Callback As Object
  Private Event As String
end sub
Public Sub Initialize(CallbackModule As Object, EventName As String)
  Callback = CallbackModule
  Event = EventName
you now have a reference to the activity in which this class-instance is initialized... And you have the eventname which should be fired in class...

So later in your class you then can use this to call a event in the calling activity with
B4X:
  If SubExists(Callback,Event&"_UpdateComplete") Then
      CallSub(Callback,Event&"_UpdateComplete")
  End If

You can leave out the event-thing... and just use
B4X:
CallSub(Callback,"subname")
but it is probably always better to do a
SubExists-call first...
 
Last edited:
Upvote 0

Roberto P.

Well-Known Member
Licensed User
Longtime User
hello DonManfred,
Thank you very much, now I try to follow your advice.
Can I access to the members of the Activity?

thanks again
rp
 
Upvote 0

Roberto P.

Well-Known Member
Licensed User
Longtime User
For example, I have two members in the class activity: number and date
the generic class I would like to access the members of the class activity and manage the data of these members (variables or other objects).

So here in practice

after creating

Private mCallback As Object

Public Sub Initialize (CallbackModule As Object ...)

mCallback = CallbackModule

end sub


sub Test()
mCallback.Numero = xxx
mCallback.Data = yy

end sub

I think you have to make do with my cast the generic object or activity here does not work like that?!

thanks
 
Upvote 0

Roberto P.

Well-Known Member
Licensed User
Longtime User
very well! if I want to read data from the parent class?
thank you very much, you've been to help
 
Upvote 0

Roberto P.

Well-Known Member
Licensed User
Longtime User
I counter-example:

activity in class I create the function that returns me the number
Sub GetNumber ()
return mNumer
end Sub


how do I call the function and return me the value

CallSub2 (Callback, "getNumber"??)

I hope it is clear
 
Upvote 0

Roberto P.

Well-Known Member
Licensed User
Longtime User
I have read the data from the class in the activity. Basically I have to write and read data from the class the activity.

hope was clear
HI
 
Upvote 0

Roberto P.

Well-Known Member
Licensed User
Longtime User
I tried to do it this way, but by mistake

in the activity
Sub GetNumero() As String
Return eNumeroFattura.Text
End Sub

in the class
If SubExists(mCallback,"GetNumero") Then
Dim ssss As String
CallSub2(mCallback,"GetNumero", ssss)

End If
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
activity:
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim myclass As mynewclass
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    myclass.Initialize(Me,"MNC")
End Sub

in mynewclass:
B4X:
Sub Class_Globals
    Dim numero As Int = 0 
  Private Callback As Object
  Private Event As String
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(CallbackModule As Object, EventName As String)
  Callback = CallbackModule
  Event = EventName
End Sub

'Gets or sets the numero
Sub getNumero As Int
  Return numero
End Sub

Sub setNumero(i As Int)
  numero = i
End Sub

You now can use
B4X:
Log(myclass.numero)
or
B4X:
myclass.numero = 35
to get or set numero in activity from the class...

Please note the magic behind getters and setters in classes.

 
Upvote 0

Roberto P.

Well-Known Member
Licensed User
Longtime User
Sai che non ho ancora capitouando usare il forum italiano e quello generico?
Grazie
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…