Developer Android
Developer Android

Reputation: 595

ProgressBar does not show immediately in android

I have a base class of an activity and a sub class which extends the base class. The superclass has a async task to perform some action. I call this by running it on the ui thread since otherwise it throws an IllegalInitializerError:

superclass.this.runOnUiThread(new Runnable() {
    public void run() {
        String p="";
        try {
            p=new asynctasker().execute().get();
        }
    }
}

In my async task:

protected void onPreExecute()
{
    // TODO Auto-generated method stub
    super.onPreExecute();
    //showDialog();
    Log.d("Now","Inside right now");
    dialog = ProgressDialog.show(class_create_event.this, "Loading1", "Please Wait");
}

However the dialog is displayed almost at the end of the request. The I am in part is printed correctly. I know that something is blocking my ui thread. But if I dont call the async task from the UI thread it throws an illegal initializer error. Is there any way out?

Upvotes: 1

Views: 162

Answers (1)

Nirali
Nirali

Reputation: 13805

You don't need to have UIthread for calling AsyncTask

Call it like this way

FetchRSSFeeds async = new FetchRSSFeeds();
async.execute();


private class FetchRSSFeeds extends AsyncTask<String, Void, Boolean> {

    private ProgressDialog dialog = new ProgressDialog(HomeActivity.this);

    /** progress dialog to show user that the backup is processing. */
    /** application context. */

    protected void onPreExecute() {
        this.dialog.setMessage(getResources().getString(
                R.string.Loading_String));
        this.dialog.show();
    }

    protected Boolean doInBackground(final String... args) {
        try {

            // Fetch the RSS Feeds from URL
            // do background process

            return true;
        } catch (Exception e) {
            Log.e("tag", "error", e);
            return false;
        }
    }

    @Override
    protected void onPostExecute(final Boolean success) {

        if (dialog.isShowing()) {
            dialog.dismiss();
        }

        if (success) {
            // Setting data to list adaptar
            setListData();
        }
    }
}

Upvotes: 1

Related Questions