Android Question Is is possible to display a MsgBoxAsync from a code module by passing the calling Activity?

JohnC

Expert
Licensed User
Longtime User
I know that it is normally not possible for a code module to display a MsgBoxAsync from within one of the code module's routines because the MsgBox needs a UI context ie. Activity (I think that's the right terminology).

But, is there a way (ie. using some Java wizardry) to pass the Activity as one of the parameters to the code module routine so that the MsyBoxAsync can use it as the context?
 

Lucas Siqueira

Active Member
Licensed User
Longtime User
I know that it is normally not possible for a code module to display a MsgBoxAsync from within one of the code module's routines because the MsgBox needs a UI context ie. Activity (I think that's the right terminology).

But, is there a way (ie. using some Java wizardry) to pass the Activity as one of the parameters to the code module routine so that the MsyBoxAsync can use it as the context?


yes, you can use a function in the code module by placing it with the ResumableSub return and calling the function using Wait For ...

see the example I set up


B4X:
'Activity Module Main ------
Sub Activity_Create(FirstTime As Boolean)
   
    Wait For (   geral.calcular( 10.50 , 25.33 , 98.12 , 3.75 )   ) Complete (retorno As Double)
    Log("retorno:"&retorno)
   
    MsgboxAsync("Resultado: "&retorno&" correto?", "Atenção")
    Wait For Msgbox_Result (result As Int)
    If result = DialogResponse.POSITIVE Then' ok
        MsgboxAsync("OK: "&retorno, "Msg")
    Else
        MsgboxAsync("OK: "&retorno, "Msg")
    End If
   
End Sub

B4X:
'Code Module geral ------

Sub calcular(n1 As Double, n2 As Double, n3 As Double, n4 As Double)  As ResumableSub
    Dim retorno As Double = 0
    If IsNumber(n1) Then retorno=retorno+n1
    If IsNumber(n2) Then retorno=retorno+n2
    If IsNumber(n3) Then retorno=retorno+n3
    If IsNumber(n4) Then retorno=retorno+n4
    Return retorno
End Sub
 

Attachments

  • teste.zip
    8.3 KB · Views: 175
Upvote 0

JohnC

Expert
Licensed User
Longtime User
I may not have described my question fully..

In your example, the "MsgBoxAync" is in your "Activity" module.

I would like to know if it's possible to have a MsgBoxAsync in a "Code" module (by somehow passing the Activity to the code module).
 
Upvote 0

angel_

Well-Known Member
Licensed User
Longtime User
I think so, try this:
Activity:
Module1.MsgInfo("Test")

Module1:
Sub MsgInfo(Txt As String)

    Msgbox2Async(Txt, "", "", "", "", Null, True)

End Sub
 
Upvote 0

Lucas Siqueira

Active Member
Licensed User
Longtime User
I may not have described my question fully..

In your example, the "MsgBoxAync" is in your "Activity" module.

I would like to know if it's possible to have a MsgBoxAsync in a "Code" module (by somehow passing the Activity to the code module).


B4X:
'Activity Module Main ------
Sub Activity_Create(FirstTime As Boolean)
    geral.calcular2( "Main" , 10.50 , 25.33 , 98.12 , 3.75 )
End Sub

Sub mensagem_calculo(retorno As Double)
    MsgboxAsync("Resultado: "&retorno&" correto?", "Atenção")
End Sub

B4X:
'Code Module geral ------

Sub calcular2( nome_modulo As String , n1 As Double, n2 As Double, n3 As Double, n4 As Double)
    Dim retorno As Double = 0
    If IsNumber(n1) Then retorno=retorno+n1
    If IsNumber(n2) Then retorno=retorno+n2
    If IsNumber(n3) Then retorno=retorno+n3
    If IsNumber(n4) Then retorno=retorno+n4
    
    Log("nome_modulo: "&nome_modulo&" | sub: mensagem_calculo | retorno: " & retorno)
    
    CallSubDelayed2( nome_modulo , "mensagem_calculo" , retorno )
End Sub
 

Attachments

  • teste2.zip
    8.5 KB · Views: 161
Upvote 0

JohnC

Expert
Licensed User
Longtime User
B4X:
'Activity Module Main ------
Sub Activity_Create(FirstTime As Boolean)
    geral.calcular2( "Main" , 10.50 , 25.33 , 98.12 , 3.75 )
End Sub

Sub mensagem_calculo(retorno As Double)
    MsgboxAsync("Resultado: "&retorno&" correto?", "Atenção")
End Sub

B4X:
'Code Module geral ------

Sub calcular2( nome_modulo As String , n1 As Double, n2 As Double, n3 As Double, n4 As Double)
    Dim retorno As Double = 0
    If IsNumber(n1) Then retorno=retorno+n1
    If IsNumber(n2) Then retorno=retorno+n2
    If IsNumber(n3) Then retorno=retorno+n3
    If IsNumber(n4) Then retorno=retorno+n4
   
    Log("nome_modulo: "&nome_modulo&" | sub: mensagem_calculo | retorno: " & retorno)
   
    CallSubDelayed2( nome_modulo , "mensagem_calculo" , retorno )
End Sub
Hi,

I very much appreciate your input, but even with your suggestion, the actual msgboxasync is located in an activity module.

What I was looking for was a way to have the MsgboxAsync in the Code module so it can be shared in different apps without having to place parts in an activity.
 
Upvote 0

Lucas Siqueira

Active Member
Licensed User
Longtime User
According to Erel, Here is an example using class to display MsgboxAsync.

I didn't know that in class I could post messages, it's new to me. :)

B4X:
'Activity Module Main ------
Sub Globals
    Dim calc As calculo
End Sub

Sub Activity_Create(FirstTime As Boolean)
    calc.Initialize
    calc.calcular3(10.50 , 25.33 , 98.12 , 3.75)
End Sub

B4X:
'Code Class calculo ------
Sub Class_Globals
End Sub

Public Sub Initialize
End Sub

Sub calcular3( n1 As Double, n2 As Double, n3 As Double, n4 As Double)
    Dim retorno As Double = 0
    If IsNumber(n1) Then retorno=retorno+n1
    If IsNumber(n2) Then retorno=retorno+n2
    If IsNumber(n3) Then retorno=retorno+n3
    If IsNumber(n4) Then retorno=retorno+n4
    
    Log("retorno: " & retorno)
    
    MsgboxAsync("Resultado: "&retorno&" correto?", "Atenção")
End Sub
 

Attachments

  • teste3.zip
    9 KB · Views: 153
Upvote 0
Top