Return value from other activity

joaomelo

Member
Licensed User
Longtime User
Hello,

I am writing an app that has 3 editext, I want to call a calculator (tapecalc) from a menu button and bring the result from the calculator to the edittext that was with the focus when the calculator menu button was clicked. I'm doing this but is not working.

B4X:
Sub Globals
   Dim txtN As EditText
   Dim txtD As EditText
   Dim txtI As EditText
   Dim Send As EditText 
End Sub

Sub Activity_Create(FirstTime As Boolean)

    txtN.Initialize("edText")
   txtD.Initialize("edText")
   txtI.Initialize("edText")
   '
   txtN.RequestFocus
End Sub

Sub Activity_Resume
    'Msgbox(TapeCalc.Resultado,"Resultado")
   If Sender = Null Then 
      Msgbox("Nulo","Sender")
   Else
      Msgbox(Send,"Sender")
      Send.Text = TapeCalc.Resultado
   End If   
End Sub

Sub edText_FocusChanged (HasFocus As Boolean)
   Send = Sender  
End Sub

Resultado is declared in Sub Process_Globals from tapecalc activity.

What am I doing wrong ?

Thanks.
 

Penko

Active Member
Licensed User
Longtime User
Just write a function for the calculator. Let's assume the calculater is a function in a Code Module:

CodeModule Calculator:

B4X:
Sub calc(a as int, b as int, c as int)

Return a * 2 / b + c

End Sub

In your Activity, just call the function:

Dim result as int : result = Calculator.code(1,2,3) ' or here you can use your variables

EditText1.Text = result

Se non ho capito vero, scrivi piu information di cosa vuoi fare.

In case I haven't understood, write more information about what you want to achieve.
 
Upvote 0

joaomelo

Member
Licensed User
Longtime User
The calculator is an activity that is called form a menu button (see "Tape Calc" in the "Basic4android Share Your Creations" forum). I can call it and the result is back in the main activity, I only can't put it in the text property of the edittext that was with focus when the calculator was called.
 
Upvote 0

joaomelo

Member
Licensed User
Longtime User
I need to test sender because in the first time this acticvity is opened the sender is null and send too. If I don't do this I receive an error message.
 
Upvote 0

joaomelo

Member
Licensed User
Longtime User
The msgbox in the activity_resumo shows the picture attached and the edittext shows nothing.
 

Attachments

  • Erro.png
    Erro.png
    31.6 KB · Views: 195
Upvote 0

klaus

Expert
Licensed User
Longtime User
Try something liks this:
B4X:
Sub Process_Globals
    Dim edtIndex As Int        : edtIndex = -1
End Sub

Sub Globals
    Dim edText(3) As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    For i = 0 To 2
        edText(i).Initialize("edText")
        edText(i).Tag = i
    Next
    edText(0).RequestFocus
End Sub

Sub Activity_Resume
    If edtIndex < 0 Then
        edText(edtIndex).Text = ""
    Else
        edText(edtIndex).Text = TapeCalc.Resultado
    End If
End Sub

Sub edText_FocusChanged (HasFocus As Boolean)
    Dim Send As EditText
    
    Send = Sender
    edtIndex = Send.Tag
End Sub
Best regards.
 
Upvote 0
Top