Android Question What's the preferred way to do a wait?

Troberg

Well-Known Member
Licensed User
Longtime User
Occasionally, especially in some GUI cases, one wants to wait a short while before continuing. For example:

Do something
Show a message
Wait 3 seconds
Show another message
Wait 3 seconds
Clear messages

In this case, it's messages, but it's not limited to that.

What is the preferred method om implementing such a wait. Sure, I could use a timer, but that would either lead to a very awkward partitioning of the code (especially if large piles of variables are needed both before and after the wait) or to the use of a DoEvents-loop, which will drain the battery.

So, what is the proper way of doing this?
 

Informatix

Expert
Licensed User
Longtime User
Personally, I would use the Threading library. First, because it's what Google recommends (in their Google Play Services library, for example, all disk and network operations are done in the background). Second, because the timer solution is just horrible for me: reading a flag constantly to know its state instead of raising an event (calling another function) when this state changes is something that I would avoid at all costs in an event-driven language (except for semaphores, but the use of a semaphore means we are using background threads).

You don't have to worry about passing your data from a function to another. Only the reference is copied, not the contents (except for primitive types, but I suppose that your data are in an array, a list, a map or something like that).
 
Upvote 0
D

Deleted member 103

Guest
That is, according to my opinion, also the best away.
B4X:
Dim thread1 As Thread
thread1.Initialise("")
thread1.Sleep(1000)
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Anything producing an event or handling events should be out of Activity_Create. It's definitely not the place for that. Activity_Create is meant to create classes instances, initialize your variables and do other preparatory works. The activity will start interacting with the user in Activity_Resume. For the same reasons, Activity_Create should not be the place for disk or network operations (that being said, it is safe to access the assets or open a database in Activity_Create).
Note that's how Google made things, it's not due to B4A.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…