Android Programming Tutorials

(Romina) #1

TUTORIAL 9


Life and Times


In this tutorial, we will make our background task take a bit longer, then


arrange to pause the background work when we start up another activity


and restart the background work when our activity regains control. This


pattern – stopping unnecessary background work when the activity is


paused – is a good design pattern and is not merely something used for a


tutorial.


Step-By-Step Instructions..........................................................................


First, you need to have completed the previous tutorial. If you are


beginning the tutorials here, or if you wish to not use your existing work,


you can download a ZIP file with all of the tutorial results, and you can copy


the 08-Threads edition of LunchList to use as a starting point.


Step #1: Lengthen the Background Work...........................................


First, let us make the background work take a bit longer, so we have a


bigger "window" in which to test whether our pause-and-resume logic


works. It is also helpful, in our case, to synchronize our loop with our


progress, so rather than counting 0 to 20 by 1 , we should count from 0 to


10000 by 200 , so the loop counter and progress are the same.


In the longTask Runnable, change the loop to look like this:


77
Free download pdf