Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Private Button1 As Button
Dim lData As List
Dim lstGet As List
End Sub
B4X:
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("1")
lData.Initialize
lData.Add("HI")
End Sub
B4X:
Sub Button1_Click
lstGet.Initialize
lstGet = lData
Log(lData.Size)
End Sub
This is not a bug. It happens because of two reasons:
1. Both variables point to the same object.
2. The assumption that calling Initialize creates a new object is incorrect though it looks like this in many cases.
The correct way to create a new object is by calling Dim. In this case, as the list is part of a type, you should do it like this:
B4X:
Dim l As List
l.Initialize
Starter.DTCView.lDataDTC = l
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Private Button1 As Button
Dim lstGet As List
Dim lstSet As List
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("1")
lstSet.Initialize
End Sub
B4X:
Sub Button1_Click
lstSet.Clear
lstSet.Add("HI")
Data(lstSet)
End Sub
Sub Data(l As List)
lstGet.Initialize
lstGet = l
Log(l.Size)
End Sub
Same result 1 and then 0. l as list is local variable and gets created with new object?