Starting the Progress Dialog

Now you need to implement the onPreExecute() method. This is the perfect place to display a progress dialog that tells the user that the trivia questions are being downloaded. The user won't be able to do anything until the questions are downloaded. Although you put the indicator in the title bar when downloading the scores earlier, you want to put the progress dialog over the game screen: ©Override protected void onPreExecute() {

pleaseWaitDialog = QuizGameActivity.this, "Trivia Quiz", "Downloading trivia questions", true, true); pleaseWaitDialog.setOnCancelListener(new OnCancelListener() { public void onCancel(DialogInterface dialog) { QuizTask.this.cancel(true);

Although we've used hardcoded strings here for clarity, a well-written application would use string resources for easy localization. A cancel listener is added. This allows the user to press the back button to cancel the dialog. When this happens, the AsyncTask cancel() method is called. This means that cancelling the dialog will now cancel the task, which will cancel the network activity.

Was this article helpful?

0 0

Post a comment