Android Question ToastMessageShow delayed by following process

Didier9

Well-Known Member
Licensed User
Longtime User
I want to display a message while a file is loading into a List so that the user knows that his button click has been received, as below:
B4X:
Sub Button1_Click
   ToastMessageShow( "Opening File...", True )
   lstRecords = File.ReadList( AppFolder, FileName.Text )
   ' do other processing that updates the display...
End Sub
Unfortunately, the ToastMessageShow message is displayed AFTER the file has been read (that takes a little while, the file can be big), which defeats the purpose.
How can I be sure the message is displayed immediately before the file is being read?
Is there another way to tell the user that the function is being processed?
 

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
Try moving your load code to its own block and then calling that block delayed.

B4X:
' DISCLAIMER:  not tested, just typed in
Sub Button1_Click
    ToastMessageShow( "Opening File...", True )
    CallSubDelayed(Me, "Load_List")
End Sub

Sub Load_List
   lstRecords = File.ReadList( AppFolder, FileName.Text )' do other processing that updates the display...End Sub
End Sub
 
Upvote 0

ronell

Well-Known Member
Licensed User
Longtime User
you can also put the readlist part at the start of the app so the list is already loaded then hitting button1 doesnt take much time
 
Upvote 0

Didier9

Well-Known Member
Licensed User
Longtime User
Try moving your load code to its own block and then calling that block delayed.

B4X:
' DISCLAIMER:  not tested, just typed in
Sub Button1_Click
    ToastMessageShow( "Opening File...", True )
    CallSubDelayed(Me, "Load_List")
End Sub

Sub Load_List
   lstRecords = File.ReadList( AppFolder, FileName.Text )' do other processing that updates the display...End Sub
End Sub

Interestingly, that does not seem to work. I still get the message after the file is processed. It seems like as long as we are jumping to lengthy code, refreshing the screen is postponed.

To be thorough, after reading the file, I plot the data on a chart so there is a fair bit of processing going on.
I cannot pre-load the data because the user picks a file from a list.
Next I will try a timer. That seems silly to delay everything just so that the message is displayed with a minimum of delay... Where is the good old fashioned hourglass when you need it :)
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
In Android, your app only has one thread, there isn't a different thread responding to UI updates. In your loop, try adding a DoEvents command based on your progress in the processing (e.g. if your loop counter MOD 100 = 0 then DoEvents or something similar).
 
Upvote 0

Didier9

Well-Known Member
Licensed User
Longtime User
In Android, your app only has one thread, there isn't a different thread responding to UI updates. In your loop, try adding a DoEvents command based on your progress in the processing (e.g. if your loop counter MOD 100 = 0 then DoEvents or something similar).

I tried that too and there does not seem to be a consistent fix. The tablet I am using is nice but not very high powered (Lenovo Yoga Tab 3) and I guess I am against one of the tough design choices the Android people had to make to save battery life. The OS will refresh the page when it's good and ready and there is not a whole lot we can do about it.
Sometimes the delay is fairly short (quasi instantaneous, as best I can tell visually), but most of the time it takes a full second to load and plot a file with 80 records...

Thanks for the suggestions anyways.
 
Upvote 0
Top