B4J Question TextChanged event execution timing : b4a ver b4j

a n g l o

Active Member
Licensed User
Longtime User
hello, please try this :

start a new b4j ui project. add a TextField to the layout
this is the code in Main :


b4j:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Button1 As B4XView
    Private TextField1 As TextField
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
End Sub

Sub Button1_Click
    checkIfToChangeText   
End Sub

Private Sub checkIfToChangeText   
    ''code with calculations
    Log(1)
    TextField1.Text="abc"
    Log(2)
    Button1.Text=TextField1.Text
    Log(3)
    ''more code enabling    / disabling view, no loops
End Sub

Private Sub TextField1_TextChanged (Old As String, New As String)
    Log(4)
End Sub

run and note the logs order - 1,2,3,4

now, one would expect that the Text_Changed event is fired right when it's changing.
as you can see - it's happening only after the changing sub code ends.
more then that - Button1.text changes to the new TextField.text before the Text_Changed fired.
what if there's a code in the Text_Changed event that for some reason will not approve the change ?
the button text already changed.....


now, please do the same with a new b4a default project.
add an EditText view to layout (instead of TextField used in b4j)
add the same code.
run and note the logs - here, the order is 1,4,3,2

my question : will it stay this way ?
 

walt61

Well-Known Member
Licensed User
Longtime User
I suspect it could happen in B4A as well. If you add a Sleep(0), the logs come in as you'd expect:
B4X:
Private Sub checkIfToChangeText   
    ''code with calculations
    Log(1)
    TextField1.Text="abc"
    Sleep(0) ' <-------- Added this to allow the related events to complete before continuing
    Log(2)
    Button1.Text=TextField1.Text
    Log(3)
    ''more code enabling    / disabling view, no loops
End Sub
 
Upvote 0

a n g l o

Active Member
Licensed User
Longtime User
thans walt61. i didnt ask how to make it work as i expecred.
its not a problem to find a work around once you spot such an issue. other then sleep.
sleep is a huge issue (as i see it) in b4j, and is subject to other post.
by the way, the sleep suppose to let the event and display get updated, but button1.text get updated before the change event log without sleep...
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
sleep is a huge issue (as i see it) in b4j
Sleep is a super important construct in B4J.

There is indeed a subtle difference in the event behavior where in B4A it is raised immediately and in B4J it is goes through the message queue.

Adding Sleep(0) in B4A event sub will make them behave in a similar way. I don't expect this behavior to change.
 
Upvote 0
Top