Java – Android: Heavy database operations with progress dialogs

Android: Heavy database operations with progress dialogs… here is a solution to the problem.

Android: Heavy database operations with progress dialogs

I’m used to Following the idiom for the doInBackground method of AsyncTask for quick insertion into a database:

    mDatabase.beginTransaction();
    try {
            //... do DB stuff

mDatabase.setTransactionSuccessful();
    } finally {
            mDatabase.endTransaction();
    }

However, if I use this idiom, my progress dialog (placed in the onPreExecute method) doesn’t appear. On the other hand, if I replace the above code with the regular mDatabase.execSQL() statement, the progress dialog is displayed, but the insertion becomes very slow. Is there any way to get me the best of both worlds (i.e. progress dialog and quick insert). Thanks!

Note: beginTransaction runs in EXCLUSIVE mode (I don’t understand what that means). Is this the reason?

Solution

Display your dialog box before launching AsyncTask (for example, via showDialog()).

Related Problems and Solutions