Sub Activity_Pause (UserClosed As Boolean)
Log("B4X Activity Pause event triggered")
B4XPages.Delegate.Activity_Pause
B4XPages.ShowPageAndRemovePreviousPages( "MainPage")
End Sub
Hi Erel,You shouldn't modify the template code. Your code cannot work.
You should instead handle the B4XPage_Background event.
Private Sub btnSelectDate_Click
Wait For (GetDateTodayOnWards) Complete ( Ticks As Long)
Log(Ticks)
End Sub
Private Sub GetDateTodayOnWards As ResumableSub
Dim cs as CSBuilder
Do While True
Wait For (Dialog.ShowTemplate( DateTemplate, "", "", "")) Complete (Result As Int)
Dim days As Int = DateUtils.PeriodBetweenInDays( DateTime.Now, DateTemplate.Date).Days
If days < 0 Then
DateTemplate.Date = DateTime.Now
Dialog.BodyTextColor=xui.Color_White
Dialog.Title = cs.Initialize.Size(20).Append("Message").PopAll
Wait For (Dialog.Show( "Select current or future date", "Ok", "", "")) Complete (Result As Int)
Else
Return DateTemplate.Date
End If
Loop
Return 0
End Sub
Hi Erel,Add some logs and find out why it doesn't exit the loop.
Sorry but no. Android, like most modern OS with a GUI is event driven. You should structure your program to not rely on looping. In the old days this was one of our biggest hurdles to overcome when retraining realtime software engineers to work on desktop systems with GUIs. They wanted to put everything in loops whereas GUI apps already have a loop running - the message loop, and all the events are called from this loop which is managed by the OS.And it is perfectly normal for an end-user to simply wait there without inputting anything on the datetemplate or keep inputting a past date and thus remain inside the loop.
Secondly, I feel that the android OS should be able to handle such conditions on its own - I mean when the process comes alive again then it has to understand that it was last inside this loop and continue to accept the end-user response which it was all the time doing before it got pushed in the background... but here the android OS is unable to remember that..... It simply hangs...
Ah! I've just realised the significance of this. This is what Android does. All Dialogs are non-modal in Android and are dismissed when the app goes to the background. You would have to program this yourself. Possibly use a flag in Globals to say when you are in the Sub and invoke the Sub again when the page becomes visible again. Or better still restructure your app to avoid the need to restart it at all.The issue is that when the app comes back to the foreground, it is unable to remember that it was inside this loop before it moved to the background.
Hi Agraham,Sorry but no. Android, like most modern OS with a GUI is event driven. You should structure your program to not rely on looping. In the old days this was one of our biggest hurdles to overcome when retraining realtime software engineers to work on desktop systems with GUIs. They wanted to put everything in loops whereas GUI apps already have a loop running - the message loop, and all the events are called from this loop which is managed by the OS.
Your supposition that it is running inside the loop is incorrect. I'm afraid that your mental model of what is happening is wrong. Invoking Wait For actually returns from your GetDateTodayOnWards Sub to the app message loop and normal Androidy things, including events, happen until the user dismisses the Dialog. This is nothing to do with Android, Resumable Subs are a B4X concept.
I have never seen a problem with putting an app in the background with a pending Wait For. Android dismissses the Dialog and the Sub is never re-entered to complete Wait For. (@Erel - a thought. Does this leave an orphaned ResumableSub instance or does it get cleaned up?) I don't see how the app can actually 'hang'. What is the manifestation of this presumed hang?
Wrong sorry. Read how Resumable Subs actually work. Android has nothing to do with properly exiting the loop because it's not a real loop if it contains a Wait For. The loop is actually unwound by the Wait For and returns to the message loop to let everything carry on as usual.This means this loop has not been properly exited by Android when the 'app was pushed to the background'...
It doesn't because Wait For is managed by B4A, not Android. Where is Android 'remembering' anything? As I said above I think that your mental model of what is happening is wrong because you haven't got the correct model of what Resumable Subs are.So if that is the case, why does android remember that 'it is inside this loop' ?
It doesn't because Wait For is managed by B4A, not Android. Where is Android 'remembering' anything? As I said above I think that your mental model of what is happening is wrong because you haven't got the correct model of what Resumable Subs are.
The app works fine when it comes back to the foreground in all other modules but when the end-user invokes the datetemplate gui again.. it does not take any click... When I press a day value in the datetemplate, the app does not respond at all.. none of the keys work... That is why I keep saying that 'it remembers something about its last presence there' (before it was pushed to the background) ...Sorry, but I don't understand what your problem actually is. You say it 'hangs' but don't qualify what you mean by hang. My suspicion is that the app is behaving as it is coded and you are expecting something different.
Ok.. I will do that shortly...You will have to post an example that recreates the problem. I suspect there is more to this than just the code in that Sub.
Thank you Agraham !Move your Dialog Initialization from B4xPage_Appear() to B4XPage_Created(). You are re-initializing Dialog when it comes back from the background while it is still displayed..
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?