Skip to content Skip to sidebar Skip to footer

How To Show Progress Dialog (in Separate Thread?) While Processing Some Code In Main Thread

I need to do the following: when app is started it runs an activity (splashActivity) which tries to create an DBHelper (SQLiteDatabase) instance which at creation time checks if d

Solution 1:

Try to declare AsyncTask as an inner class for the activity, instantiate and execute it there. I think the problem is in context given to ProgressDialog when you are creating it, your logic for doing this is too complicated. Declaring AsyncTask in activity is more common practice.

Then, AsyncTask must do all you need with DB in doInBackground method

publicclassSplashActivityextendsActivity {
    /** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        newRestoreDBTask().execute();
    }

}

privateclassRestoreDBTaskextendsAsyncTask <Void, Void, String>
{
    privateProgressDialog dialog;

    @OverrideprotectedvoidonPreExecute()
    {
        dialog = ProgressDialog.show(
            SplashActivity.this,
            getString(R.string.progress_wait),
            getString(R.string.progress_db_installing), 
            true);
    }

    @OverrideprotectedStringdoInBackground(Void... params)
    {
        // do all the things with DBDBHelper dbHelper = newDBHelper(SplashActivity.this);
        dbHelper.close();
        return"";
    }

    @OverrideprotectedvoidonPostExecute(String result)
    {
        dialog.dismiss();
    }
}

Post a Comment for "How To Show Progress Dialog (in Separate Thread?) While Processing Some Code In Main Thread"