Just an idea:
If you can somehow make sure that the activity process message loop doesn't get killed and recreated, then you can have a Resumable Sub running "separate" to your main program, by using Sleep(1) to occasionally yield from your "background" task to the main program.
I've done it in B4J, but and just tested it in B4A, and it works fine as a demo, as long as the activity remains intact.
The background task sends information back to the rest of the program via a global Result() As Object.
The first element of the array is the task status. In B4J I used a Boolean that is set True when the task is finished, but in the demo below, I used an Int to signify percentage complete, set to 100 when the task is finished.
Result() has to be Dim'ensioned (allocated) before calling the background task. It doubles as an indication that the background task is running: if it is only 1 element long, then the task is not running; if it is more than 1 element long, then the task is (or should be) running.
Good luck. Like I said, it's just an idea. If you work out how to make sure that the activity doesn't get killed - which I assume would take down the background task with it - I'd be interested in how you did that.
Code:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Private xui As XUI
Dim Result1(1) As Object
'Dim Result2(1) As Object 'multiple background tasks, no problem!
'Dim Result3(1) As Object
'Dim Result4(1) As Object
Dim CheckTasksTimer As Timer
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Layout")
CheckTasksTimer.Initialize("CheckTasksTimer", 1000)
CheckTasksTimer.Enabled = True
End Sub
Sub CheckTasksTimer_Tick
Log("Tick")
If Result1.Length <> 1 Then
If Result1(0) = 100 Then
Log("Task1 finished!")
For I = 0 To 3
Log("Result(" & I & ") = " & Result1(I))
Next
Dim Result1(1) As Object
Else
Log("Task1 in progress... " & Result1(0) & "%")
End If
End If
'repeat similar for other background tasks eg
'If Result2.Length <> 1 Then
End Sub
Sub Button1_Click
'''xui.MsgboxAsync("Hello world!", "B4X")
Log("Starting background task (from Button1_Click)")
Dim Result1(4) As Object
BackgroundTask(Result1)
Log("Background task started")
End Sub
Sub BackgroundTask(Result() As Object)
Result(0) = 0
Dim BigCount As Long = 0
Dim BigSum As Double = 0
Dim BigTime As Int = 0
Dim T1 As Long
Dim T2 As Long
For I = 1 To 10000
Result(0) = (99 * I / 10000).As(Int)
T1 = DateTime.Now
Sleep(0) 'this is where background task temporarily yields to main program message loop
T2 = DateTime.Now
BigTime = BigTime + (T2 - T1)
For J = 1 To 10000
BigCount = BigCount + I * J
BigSum = BigSum + Sqrt(I * I + J * J)
Next
Next
Result(1) = BigCount
Result(2) = BigSum
Result(3) = BigTime
Result(0) = 100 'order shouldn't matter but why risk it?
End Sub
Code:
Logger connected to: HMD Global Nokia C01 Plus
** Activity (main) Create (first time) **
** Activity (main) Resume **
Tick
Tick
Tick
Tick
Starting background task (from Button1_Click)
Background task started
Tick
Task1 in progress... 0%
Tick
Task1 in progress... 12%
Tick
Task1 in progress... 38%
Tick
Task1 in progress... 64%
Tick
Task1 in progress... 90%
Tick
Task1 finished!
Result(0) = 100
Result(1) = 2500500025000000
Result(2) = 7.652604958254282E11
Result(3) = 111
Tick
Tick
Tick
Tick