Search code examples
androidandroid-studioandroid-asynctaskandroid-alertdialog

Make an alertbox appear when async task is running and remove when task is finished?


I am developing a mobile application which inserts a list of words to a SQLite database and I use async task to insert the data to the database and data is inserted to the database when application opens so what I want is to show a alert box with a message.

I tried using this,

*

protected void onProgressUpdate(Object[] values) {
 super.onProgressUpdate(values);
    builder.setCancelable(true);
    builder.setTitle("Title");
    builder.setMessage("Adding....");
    builder.show();
}

*

But this won't appear when I start the application. I want to get the alert box saying "Adding data" and when the async task finishes I want to hide the alert box. Please help me with this.


Solution

  • Alert dialog works on ui thread.You cannot show it from background thread. So better if you show the alert dialog before executing the task or while creating the task and on post execution you can dismiss the alert dialog while your task execution completes and you come to foreground.