Android Question What is the best technique to return variables between activities

netsistemas

Active Member
Licensed User
Longtime User
Attach a demo for my question.

I need return a user definied data from one activity to previous actity.
If the data is primitive (string, int, and other), all is ok, but if my var is definid for me, the value are missing when do a initialize. (i do this for clear var for future calls in other program points)

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Type TD_SAMPLE (Code As Int, Name As String )
    Public Rt_Data As TD_SAMPLE
End Sub

Code in Main activity

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private EditText1 As EditText
    Private cmdGetData As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout1")
End Sub

Sub Activity_Resume
    cmdGetData.Text = frmGetData.Rt_Data.Name
    EditText1.Text = frmGetData.Rt_Data.Code
  
    cmdGetData.Tag = frmGetData.Rt_Data
  
    'THIS CODE REMOVE THE VALUE INTO TAG. HOW COPY DATA TO TAG?   Primitive vars are ok, but user defined data crack.
    'i know the cause, but, whats is the best thecnic?
  
  
    'i do this for clear the var, if i use in other parts of code. but, i do a initialice before call - view: cmdGetData_Click
    frmGetData.Rt_Data.Initialize
  
  
  
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub




Sub cmdGetData_Click
    frmGetData.Rt_Data.Initialize
    StartActivity(frmGetData)
End Sub

Sub Button1_Click
    Dim V As TD_SAMPLE
  
    Try

        v = cmdGetData.Tag
  
        MsgboxAsync (V.Name ,V.Code )
          
    Catch
        Log(LastException)
        MsgboxAsync ("Error. there are nothing in tag","Error")
    End Try
End Sub

Code in Activity to return a var.
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Type TD_SAMPLE (Code As Int, Name As String )
    Public Rt_Data As TD_SAMPLE
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

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("Layout")

End Sub



Sub Button1_Click
    Rt_Data.Code = DateTime.Now
    Rt_Data.Name = "SAMPLE NAME"
    Activity.Finish
End Sub



Best view sample.
 

Attachments

  • DemoGetData.zip
    5 KB · Views: 228

Cadenzo

Active Member
Licensed User
Longtime User
I think this line 21 in Activity_Resume (Main) is called before any frmGetData.Rt_Data.Initialize.
Resume is also called in the beginning after Activity_Create, not only, when it resumes again, coming back from another form.

Also not correct:
B4X:
Code as Int 'DateTime.Now is type Long, not Int
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
I would place your type declaration and the variable in your Starter service. Then in your main Activity_Create (pseudocode):
B4X:
If Starter.Rt_Data.IsInitalized Then
    If Starter.Rt_Data.Code = 0 Then
         ' call activity two to get data, etc.
    else
        ' carry on here
    End If
Else
     ' call activity 2 to get data, etc.
End If
 
Upvote 0

netsistemas

Active Member
Licensed User
Longtime User
thanks, but..
my question is not this.
(long, int, is irrelevant for my question, only is a sample).

Only: who pass var beetwen process and how traslate a personal data type to a other var, and clear the original var.
 
Upvote 0

netsistemas

Active Member
Licensed User
Longtime User
mi app, is old and i can't change all logical.
there are some thenic to 'clone' a var.
No reference to previour var, NEW values var: copy values to new var.
 
Upvote 0

Cadenzo

Active Member
Licensed User
Longtime User
So, if I understand now, you have in some activities in the app some different vars with the same defined type that contains (different) user data. And with Rt_Data = frmGetData.Rt_Data you get only reference and when initialize the one, the other is also cleared. Is that the problem?

In that case I would write a short sub, anywhere in a module or public class, that clones the var
B4X:
Sub CopyVar(t as TD_SAMPLE) as TD_SAMPLE
 dim t2 as TD_SAMPLE
 t2.Initialize
 t2.Name = t.Name
 t2.Code = t.Code
 Return t2
End Sub
 
Upvote 0

netsistemas

Active Member
Licensed User
Longtime User
YES, but.. if you add new var, into personal type, you must remember change this!!

and if one var, if other personal type (personal type, into personal type).... more problems.

thanks, the final solution is near!!
 
Upvote 0
Top