Android Question resumable sub and Sleep vs. app pause/resume?

Dave O

Well-Known Member
Licensed User
Longtime User
How does a resumable sub using Sleep interact with the activity pausing and resuming?

For example, I'm working on a poker app that uses sleep(250) to pause between each player's bet. Works great while the app is running.

However, if I switch out of my app while it's stepping through the players, then return to the app, the sub doesn't appear to resume.

(If I was using the old method of timers, I would pause/resume the timer when my app was paused/resumed.)

Thanks!
 

ilan

Expert
Licensed User
Longtime User
If I can use Sleep(), that gives me a more elegant solution than a timer does (and makes it easier to follow the program flow when I revisit the code later).

But if Sleep doesn't work out, it's back to a timer, agreed.

You could use sleep like this.

Before calling sleep create a file called sleep.txt in file.dirinternal then after sleep() delete that file. Now when u return to the activity perform a check if that file exists if true that means app was paused in sleep() action now you can return and call that resumable sub again.
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
I have learned with android that if the Activity is not in front of me on the main screen, its "Destroyed or Dormant" so I treat it as such. Thats why I keep all critical code/variables in code modules/services, with CallSubDelayed back to the activity.

I have never had a fault using this method. So when the operator brings it back up on the main screen, the activity is re-created, but its re-created with the data that still exists in the service/code modules.

Also, food for thought: The activity gets destroyed and re-created when rotating the device between portrait/landscape as well....
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…