Android Tutorial Voice Recognition and activity lifecycle

This might be obvious to some but it caused me some grief so hopefully I can spare someone else some grief.
When using Voice Recognition I have found that it defies the activity lifecycle. What I mean by that is that when you call the listen
method your program calls Activity_Pause, which is normal, but when the Result is triggered the program doesn't call Activity_Resume
until the Result sub is done.
The Result sub can call other subs within your program including manipulating views. This is where problems can occure if you change something that will be again changed when the Resume Sub is called.
What I find is the best method to handle Voice input is to declare 2 Process_Globals variables. Use one as a flag and the other to
hold the Voice result. In the Result sub set the flag to true and fill the variable with the result. eg flag = True: res = texts.Get(0).
Then in the Activity_Resume Sub do what you need to put everything back as you prepared for in the Activity_Pause Sub. Then use an IF
statement to check if the flag variable is true, if it is then set it to false and process the Voice results.

This shows the program flows after Voice Recognition is called, phone library is needed.

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
Dim VR As VoiceRecognition
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
Log ("Create")
VR.Initialize("VR")
VR.Listen
End Sub

Sub Activity_Resume
Log("Resuming")
End Sub

Sub Activity_Pause (UserClosed As Boolean)
Log("Pausing")
End Sub

Sub VR_Result (Success As Boolean, Texts As List)
Log ("VR.Results")   
test1
End Sub
Sub test1
Log("test1")
test2
End Sub
Sub test2
Log("test2")
End Sub

Here is the log...
** Activity (main) Create, isFirst = true **
Create
** Activity (main) Resume **
Resuming
** Activity (main) Pause, UserClosed = false **
Pausing
running waiting messages (1)
VR.Results
test1
test2
** Activity (main) Resume **
Resuming
** Activity (main) Pause, UserClosed = true

**
Pausing

This is I prefer to handle Voice Recognition.....

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
Dim VR As VoiceRecognition
Dim Flag As Boolean
Dim res As String
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
Log ("Create")
VR.Initialize("VR")
VR.Listen
End Sub

Sub Activity_Resume
Log("Resuming")
If Flag = True Then
flag = False
test1
End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
Log("Pausing")
End Sub

Sub VR_Result (Success As Boolean, Texts As List)
Log ("VR.Results")   
Flag = True
res = texts.Get(0)
End Sub

Sub test1
Log("test1")
test2
End Sub

Sub test2
Log("test2")
End Sub
 
Top