Android Question Problem with Activity_Create and Resumable Sub

GaNdAlF89

Active Member
Licensed User
Longtime User
Hi to all!
I have an old project and I needed to update it, but now I have sync problems with subs in Activity_Create and Activity_Resume, in which now there are many resumable subs.
My code is like this:
B4X:
Sub Activity_Create (FirstTime As Boolean)
    
    Wait For (Sub1) Complete (Result As Object)
    
    Wait For (Sub2) Complete (Result As Object)
    
End Sub

Sub Activity_Resume

    Wait For (Sub3) Complete (Result As Object)
    
End Sub
Sub1, Sub2 and Sub3 are all "As ResumableSub" and return null.

The problem that I have is that after execution of Sub1, the code flow executes Sub3.
How can I solve it? Many thanks to all
 

OliverA

Expert
Licensed User
Longtime User
Move all Wait For's to Activity_Resume. This is just how resumable subs work with Android's app life cycle. Android calls Activity_Create and upon it finishing, Activity_Resume is called (in B4A's case). With the first Wait For in Activity_Create, you are pretty much exiting/finishing Activity_Create, upon which Activity_Resume is called. Once your first Wait For completes, Activity_Create is resumed at where it waited, but in Android's life cycle, Activity_Create has already finished.

You could also create a global flag that is set upon finishing the Wait For on Sub2. In Activity_Resume, you could check for that flag (right at the start) and if it is not set use a while loop and sleep to periodically check on the flag. I would only do this if the first two Wait Fors have to happen in the Activity_Create portion of the app. Otherwise, I would move them all to Activity_Resume and use a flag to see if I need to run the first two or if I can skip straight to the third Wait For.
 
Upvote 0

GaNdAlF89

Active Member
Licensed User
Longtime User
Thank you so much for your reply.
Now it's all clear for me! I thought that it was a strange behaviour of my code, because I didn't know that:
With the first Wait For in Activity_Create, you are pretty much exiting/finishing Activity_Create, upon which Activity_Resume is called. Once your first Wait For completes, Activity_Create is resumed at where it waited, but in Android's life cycle, Activity_Create has already finished.

Meantime, I thought a workaround for this, and I see that it's the same of your solution. I will proceed in this way.
Many thanks again!
 
Upvote 0
Top