Android Question Wait for not working

AndroidMadhu

Active Member
Licensed User
Hello,
wait for is not working properly when I am putting the code in Activity_create()
The below is the code at Activity_Create():
B4X:
Dim jobPayKey As HttpJob
jobPayKey.Initialize("",Me)
jobPayKey.Download("http://xxxxxxxxx/rzrPayKeys")
Wait For (jobPayKey) JobDone(jobPayKey As HttpJob)
If jobPayKey.Success Then
Dim JSON As JSONParser
JSON.Initialize(jobPayKey.GetString)

Dim Map1 As Map
Dim m As Map
Map1 = JSON.NextObject

Dim MenuItems As List
MenuItems = Map1.Get("payKey")

m = MenuItems.Get(0)
keyId=m.Get("key_id")
keySecret=m.Get("key_secret")
End If

Please note that I am working in Debug Mode and after executing Wait for the control directly moves to Activity_resume().
Remaining Codes after Wait For is NOT executing in Activity_create()

Please advice.

Thanks
 

Star-Dust

Expert
Licensed User
Longtime User
Hello,
wait for is not working properly when I am putting the code in Activity_create()
The below is the code at Activity_Create():
B4X:
Dim jobPayKey As HttpJob
jobPayKey.Initialize("",Me)
jobPayKey.Download("http://xxxxxxxxx/rzrPayKeys")
Wait For (jobPayKey) JobDone(jobPayKey As HttpJob)
If jobPayKey.Success Then
Dim JSON As JSONParser
JSON.Initialize(jobPayKey.GetString)

Dim Map1 As Map
Dim m As Map
Map1 = JSON.NextObject

Dim MenuItems As List
MenuItems = Map1.Get("payKey")

m = MenuItems.Get(0)
keyId=m.Get("key_id")
keySecret=m.Get("key_secret")
End If



Please note that I am working in Debug Mode and after executing Wait for the control directly moves to Activity_resume().
Remaining Codes after Wait For is NOT executing in Activity_create()

Please advice.

Thanks
Today is a holiday nobody works even Wait For ... Joke. ???
Don't put the wait For in Create. Put it in a sub which you then call it on Create.
Otherwise it goes into the resume because it stops the creation of the Activity

B4X:
Sub Activity_Create(FirstTime As Boolean)
   '  your code
   Load
End Sub

Sub Load
    Dim jobPayKey As HttpJob
    jobPayKey.Initialize("",Me)
    jobPayKey.Download("http://xxxxxxxxx/rzrPayKeys")
    Wait For (jobPayKey) JobDone(jobPayKey As HttpJob)
    If jobPayKey.Success Then
        Dim JSON As JSONParser
        JSON.Initialize(jobPayKey.GetString)

        Dim Map1 As Map
        Dim m As Map
        Map1 = JSON.NextObject

        Dim MenuItems As List
        MenuItems = Map1.Get("payKey")

        m = MenuItems.Get(0)
        keyId=m.Get("key_id")
        keySecret=m.Get("key_secret")
    End If
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Move all that code to a new routine (sub) and call it from inside Activity_Create using:
CallSubDelayed(Me, "RoutineName")
Never needed.

What you are describing is the expected behavior. Wait For doesn't hold the thread. It returns and later when the result will arrive the sub will be resumed. It will not stop Activity_Resume from being called.
Put a breakpoint in line 5 and you will see that it is eventually called. Don't forget to release the job.

BTW, as always, switching to B4XPages will automatically solve all these issues.
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
The below is the code at Activity_Create():
You are only checking for any Success Result but not for Success = false. You may miss any errormessage.
 
Upvote 0
Top