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
public class SplashActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
new RestoreDBTask().execute();
}
}
private class RestoreDBTask extends AsyncTask <Void, Void, String>
{
private ProgressDialog dialog;
@Override
protected void onPreExecute()
{
dialog = ProgressDialog.show(
SplashActivity.this,
getString(R.string.progress_wait),
getString(R.string.progress_db_installing),
true);
}
@Override
protected String doInBackground(Void... params)
{
// do all the things with DB
DBHelper dbHelper = new DBHelper(SplashActivity.this);
dbHelper.close();
return "";
}
@Override
protected void onPostExecute(String result)
{
dialog.dismiss();
}
}
Post a Comment for "How To Show Progress Dialog (in Separate Thread?) While Processing Some Code In Main Thread"