Wish Wait For "Activity"

Discussion in 'Bugs & wishlist' started by JohnCody, Jan 14, 2019.

  1. JohnCody

    JohnCody Well-Known Member Licensed 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:

    Code:
    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: Jan 14, 2019
    Patrick Clark likes this.
  2. Patrick Clark

    Patrick Clark 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.
     
  3. DonManfred

    DonManfred Expert Licensed User

    You should start a new thread for your issue.
     
  4. Patrick Clark

    Patrick Clark Member Licensed User

    Why? I am supporting @JohnCody request not asking for help!
     
  5. Cableguy

    Cableguy Expert Licensed 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
     
  6. JohnCody

    JohnCody Well-Known Member Licensed 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: Jan 14, 2019
  7. LucaMs

    LucaMs Expert Licensed User

    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 likes this.
  8. Cableguy

    Cableguy Expert Licensed 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...
     
  9. LucaMs

    LucaMs Expert Licensed User

    my question, however, was not sarcastic; I would like an example where he needs something like that.
     
  10. JohnCody

    JohnCody Well-Known Member Licensed User

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

    Code:
    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: Jan 14, 2019
  11. LucaMs

    LucaMs Expert Licensed 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.

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

    MarkusR Well-Known Member Licensed User

    this should work
    Code:
    Sub Button1_Click

        
    Log("Start Help")
        
    StartActivity(HelpActivity)
        
    Wait For Activity_Resume
        
    Log("After WaitFor")
    End Sub
    in the help activity
    Code:
    Sub Button1_Click
       
        
    Activity.Finish
       
    End Sub

     
  13. MarkusR

    MarkusR Well-Known Member Licensed User

    .. there exists also StartActivityForResult in the android sdk.
     
  14. JohnCody

    JohnCody Well-Known Member Licensed User

    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?

    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: Jan 14, 2019
  15. JohnCody

    JohnCody Well-Known Member Licensed User

    Now that's an interesting solution!

    I'll give that a try...
     
    Martin Fernandez and MarkusR like this.
  16. JohnCody

    JohnCody Well-Known Member Licensed 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: Jan 14, 2019
    MarkusR likes this.
  17. MarkusR

    MarkusR Well-Known Member Licensed 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.
    Code:
    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
     
    Peter Simpson likes this.
  18. Cableguy

    Cableguy Expert Licensed 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
     
    Peter Simpson likes this.
  19. JohnCody

    JohnCody Well-Known Member Licensed User

    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!
     
    MarkusR likes this.
  20. Bob Spielen

    Bob Spielen Active Member Licensed User

    Really nice solution!
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice