Android Question Calling a sub in calling activity from a class?

Arf

Well-Known Member
Licensed User
Longtime User
So I have this msgBox class I made, but I need to call a function in the activity in which the class instance was initialised and called, from within the class. I thee are a few different activities which I need to notify, depending on which activity created the messagebox.

I first though to just use a bunch of these, as I am stupid:

B4X:
Private Sub NotifySender
    If MustNotify = True Then
        CallSubDelayed2(ForcedTest, "MsgBoxResponse",ButtonResponse)
                CallSubDelayed2(RelaxedTest, "MsgBoxResponse",ButtonResponse)
                CallSubDelayed2(Patients, "MsgBoxResponse",ButtonResponse)
    End If
End Sub

but then I realised I was clever enough to realise that callsubDelayed will fire up those Activities instead of doing nothing with the ones that aren't active - it's only the active on I wish to notify.

So, I created this msgBox class by modifying Margret's CToast class, and in the calling parameters, there happen to be
Sub Initialize(Activity As Activity, Module As Object, Act_H As Int, Act_W As Int)

My question is, can I use any of those in some clever way to call a sub in the activity that I called the class ShowMsgBox function from?

Thanks and have a nice weekend.
 

SteveTerrell

Active Member
Licensed User
Longtime User
So I have this msgBox class I made, but I need to call a function in the activity in which the class instance was initialised and called, from within the class. I thee are a few different activities which I need to notify, depending on which activity created the messagebox.

I first though to just use a bunch of these, as I am stupid:

B4X:
Private Sub NotifySender
    If MustNotify = True Then
        CallSubDelayed2(ForcedTest, "MsgBoxResponse",ButtonResponse)
                CallSubDelayed2(RelaxedTest, "MsgBoxResponse",ButtonResponse)
                CallSubDelayed2(Patients, "MsgBoxResponse",ButtonResponse)
    End If
End Sub

but then I realised I was clever enough to realise that callsubDelayed will fire up those Activities instead of doing nothing with the ones that aren't active - it's only the active on I wish to notify.

So, I created this msgBox class by modifying Margret's CToast class, and in the calling parameters, there happen to be
Sub Initialize(Activity As Activity, Module As Object, Act_H As Int, Act_W As Int)

My question is, can I use any of those in some clever way to call a sub in the activity that I called the class ShowMsgBox function from?

Thanks and have a nice weekend.

When I want to "reply" from a class to the calling routine (which could be one of many) i pass the Module and subroutine name to the class method (which it saves in local variables) then when it is time to reply use that saved information. If you have to pass a number of variables (such as the activity, the message to display etc.) to the method add then to the passed parameter list or put them in a Map.

B4X:
Class_Globals
Private subName As String
Private module As Object
End Sub

Public Sub ClassMethod(moduleToReplyTo as Object, subToCall as String)
module = moduleToReplyTo
subName = subToCall
' show msg panel etc

Private Sub NotifySender
CallSubDelayed2(module, subName, etc
 
  • Like
Reactions: Arf
Upvote 0
Top