Wish Wait For "Activity"

JohnC

Expert
Licensed User
Longtime User
The "Wait For" addition to the language has saved a lot of time - no longer do I have to create multiple subs for async operations and with dialogs and some other items.

But, one thing that I often find I need to do is to display an activity and pause program flow until that activity closes.

Right now I have to either create a class for each activity that I want to use this way, or I have to keep track of where I display the activity from so I'll know where to return program flow back to.

But, what would save a lot of time is if Wait For supported Activities, maybe like this:

B4X:
Wait For "Help"

This would simply display the Help activity and program execution would pause until the Help activity closes (by the user or code). Thats it - Simple!

This would allow for very complex dialogs made from activities. And no parameters would need to be in the Wait For statement because any values selected/entered by the user of the dialog can be easily handled using as many global vars as needed.

Adding support for Activities to Wait For would make my code so much easier to read, write and debug!
 
Last edited:

Patrick Clark

Active Member
Licensed User
And me for that request.

I managed to sort out my program flow with a Case statement in Main->Resume but now I am struggling as I have an activity that manages FTP downloads for me with screen display and progress etc.

Problem is exactly as you describe @JohnCody. The application continues with the statement after the "StartActivity" meaning the FTP hasn't finished before continuing.

This is the biggest problem I have coding android apps.
 

DonManfred

Expert
Licensed User
Longtime User

Cableguy

Expert
Licensed User
Longtime User
Clearly you don't understand neither the underlying workings of the "wait for" neither the way ANDROID handles activities...
You should start by reading the "Activities Life Cycle tutorial"... It has well aged over the years, just like a good wine... After reading it, I'm sure it will bring you a complete new line of thought and logic on how to manage multiple activities
 

JohnC

Expert
Licensed User
Longtime User
I know exactly how activities and async objects work like dialog boxes and http jobs.

I often find myself needing to display a common activity from many different locations in my app, and this "wish" would simply make doing that so much easier and cleaner.

Erel added the "Wait For" function for a reason - to make it much easier to work with async stuff instead of having to create separate subs to handle all the async events and state machine, which makes programming more complex.

So, I just wanted to see if it was possible for the "Wait For" function to support activities.

Are you saying that it would be impossible for "Wait For" to support activities?
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
I know exactly how activities and async objects work like dialog boxes and http jobs.

But Erel added the "Wait For" function for a reason - to make it much easier to work with async stuff instead of having to create separate subs to handle all the async events and state machine, which makes programming more complex.

I often find myself needing to display a common activity from many different locations in my app, and this "wish" would simply make doing that so much easier and cleaner.

So, I just wanted to see if it was possible for the "Wait For" function to support activities.

Are you saying that it would be impossible for "Wait For" to support activities?
Where would you like to wait for an Activity to close? You could (maybe) do it only from within a service module, certainly not from another Activity; in this second case it is normal to return to the calling Activity.
 

Cableguy

Expert
Licensed User
Longtime User
Waiting for an activity, or anything else for this matter, is pretty much as sending someone else fetch the nails while you hold the hammer. In the case of an activity, is like waiting for the tree to grow, be cut and delivered to you while you hold the hammer with one hand and the nail with the other.
The OS already pauses the activity sent to background while it runs the activity called to foreground. You already can share values between activities using the Process_Globals. You already can do some "weird" stuff using services, that keep on working even in background... In fact, that's what they're ment for...
 

JohnC

Expert
Licensed User
Longtime User
I need to look into this more, because back when I was learning B4A I remember that if I had code like this:

B4X:
StartActivity("Help")

LoadNewJob

Either the "LoadNewJob" sub would run while the Help Activity was displayed, or that after the Help activity was closed, the LoadNewJob didn't execute - I forget which of these two things happened, but my point is that from that moment on I stayed away from expecting it to work like that.

So, I'll need to look into this more...
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
Either the "LoadNewJob" sub would run while the Help Activity was displayed,
LoadNewJob will not start. When you close the help activity, the calling activity will be resumed (right in its Activity_Resume).
You could use a global variable (HelpShown) and check it in the Activity_Resume, and then act accordingly.

A better way is to use a panel for your help and load texts (and a layout, if you want) as needed.
 

MarkusR

Well-Known Member
Licensed User
Longtime User
this should work
B4X:
Sub Button1_Click

    Log("Start Help")
    StartActivity(HelpActivity)
    Wait For Activity_Resume
    Log("After WaitFor")
End Sub

in the help activity
B4X:
Sub Button1_Click
   
    Activity.Finish
   
End Sub


** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Start Help
** Activity (main) Pause, UserClosed = false **
** Activity (helpactivity) Create, isFirst = true **
** Activity (helpactivity) Resume **
** Activity (helpactivity) Pause, UserClosed = true **
** Activity (main) Resume **
After WaitFor
 

MarkusR

Well-Known Member
Licensed User
Longtime User
.. there exists also StartActivityForResult in the android sdk.
 

JohnC

Expert
Licensed User
Longtime User
LoadNewJob will not start. When you close the help activity, the calling activity will be resumed (right in its Activity_Resume).
You could use a global variable (HelpShown) and check it in the Activity_Resume, and then act accordingly.

That's exactly my point. Since control would always return to the calling activity's RESUME sub (and not the sub I displayed the activity from), I would need to implement a state machine (using global vars) to keep track of what sub displayed the help activity to then know where I need to jump control back to. This is very messy, hard to follow and hard to debug.

I wonder how many other users are forced to setup a state machine by using global vars in their "Activity_Resume" sub to manage situations like this?

Wouldn't you agree that if you could have a single line of "Wait For Help", that it would be so much cleaner and easier of a solution?

A better way is to use a panel for your help and load texts (and a layout, if you want) as needed.
Then I would have to add a panel to each and every activity that I want to display the help activity in which would be very time consuming.

I know that there are probably many different ways to address this issue and I appreciate all the suggestions. But, the whole point of my "wish" was to have a single-line solution.

Erel didn't have to add the "Wait For" function - he could have let everyone do things the "ANDROID" way and deal with all the async subs and state managing. But, he wanted to make it easier to work with async stuff, and my wish is just expanding on that same principal.
 
Last edited:

JohnC

Expert
Licensed User
Longtime User
B4X:
Sub Button1_Click
    Log("Start Help")
    StartActivity(HelpActivity)
    Wait For Activity_Resume
    Log("After WaitFor")
End Sub

Now that's an interesting solution!

I'll give that a try...
 

JohnC

Expert
Licensed User
Longtime User
BRAVO!!!

That seems to work very nicely and is exactly what I was looking for:
  • No global vars!
  • No need to wrap the called Activity in a class!
  • No additional async subs!
  • Can call any activity from any other activity and will pause/resume execution without needing a statemachine!
  • Simple, clean 2 line solution!
 
Last edited:

MarkusR

Well-Known Member
Licensed User
Longtime User
but notice if u press the back key it would also resume.

other solution
if u need wait until a flag is set u can use this.
the "sleep" will continue the event loop.
B4X:
Closed = False

Wait For(WaitClose) Complete (Result As Boolean)

Sub WaitClose As ResumableSub
   
    Do Until Closed =True
        Sleep(0)
    Loop

    Return True
   
End Sub

Sub ButtonOk_Click
  
   Closed=True
  
End Sub
 

Cableguy

Expert
Licensed User
Longtime User
Now is time for @MarkusR to post this solution as a snippet so that this doesn't get lost in the midst of this "wish" thread
 

JohnC

Expert
Licensed User
Longtime User
but notice if u press the back key it would also resume.

other solution
if u need wait until a flag is set u can use this.
the "sleep" will continue the event loop.
B4X:
Closed = False

Wait For(WaitClose) Complete (Result As Boolean)

Sub WaitClose As ResumableSub
  
    Do Until Closed =True
        Sleep(0)
    Loop

    Return True
  
End Sub

Sub ButtonOk_Click
 
   Closed=True
 
End Sub

I could also just intercept the back key in the activity_keypress and ignore it or process it some other way.

But thank you for your excellent solution!
 
Top