Android Programming Tutorials

(Romina) #1
Life and Times

icon.setImageResource(R.drawable.ball_yellow);
}
else {
icon.setImageResource(R.drawable.ball_green);
}
}
}
}

Extra Credit.................................................................................................


Here are some things you can try beyond those step-by-step instructions:



  • Have the progress position be persisted via onSaveInstanceState().
    When the activity is started in onCreate(), see if the background


work was in progress when the activity was shut down (i.e., progress
further than 0 ), and restart the background thread immediately if it

was. To test this, you can press <Ctrl>-<F12> to simulate opening the
keyboard and rotating the screen – by default, this causes your

activity to be destroyed and recreated, with onSaveInstanceState()
called along the way.


  • Try moving the pause/resume logic to onStop() and onStart().


Further Reading..........................................................................................


You can find material on the topics shown in this tutorial in the "Handling


Activity Lifecycle Events" chapter of The Busy Coder's Guide to Android


Development.


You are also strongly encouraged to read the class overview for Activity in


the JavaDocs.


86
Free download pdf