4

I have an activity, composed of an AsyncTask aiming to launch a request when the user clicks on the button. I have been looking for answers, but I didn't find the same problem, or it didn't the same for me. The code is doing what I want, but the ProgressDialog looks blocked as the spinner is not turning sometimes (almost all the time).

When I click on the button :

AsyncTask is launched -> showDialog() is called onPreExecute -> startSearch ( SearchManager launches a new AsyncTask with in the doInBackground there is a heavy call with network ) -> doInBackground in Activity waits for SearchManager to be loaded -> display.

Code for button :

button_search.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                new SearchTask().execute();

            }
        });

Code for AsyncTask in Search Activity :

private class SearchTask extends AsyncTask<Void,Void,Void>{

        @Override
        protected void onPreExecute(){
            showDialog(DIALOG_LOADING_ID);

            searchManager.startSearch();
        }

        @Override
        protected Void doInBackground(Void... params) {


            while(searchManager.isLoading()){
                try {Thread.sleep(150);} catch(Exception e){};
            }


            return null;
        }

        @Override
        protected void onPostExecute(Void ret){
            try {dismissDialog(DIALOG_LOADING_ID);} catch (Exception e){};

            if ( searchManager.errorOccurred() ){
                //Error
            } else {
                //No Error

            }   
        }

Code for SearchManagerAsyncTask : which is directly launched by startSearch

protected class SearchAsync extends AsyncTask <Void,Void,Void>{

        @Override
        protected Void doInBackground(ComSearchAds... datas) {
            global.getDataManager().doSearch();
                        //... When finished
                        setIs_loading(false);
                }
}

I'm apparently doing something wrong, but can't find what and how to avoid this. Thanks for your help !

SOLUTION :

Finally, it appears that the not spinning ProgressDialog was because I was using the same instance of ProgressDialog and

showDialog(DIALOG_LOADING_ID); 
//doInBackground
dismissDialog(DIALOG_LOADING_ID);

used with causes problem, I changed to

removeDialog(DIALOG_LOADING_ID)

and now it's working fine.

Thanks All, and hope it can help someone someday !

Camille R
  • 1,433
  • 2
  • 17
  • 29

3 Answers3

0

Could you please try to add a ProgressBar control in your layout file and set its Visible to true when starting async task and set to gone when end of process reached. Below is the code for the same.

<ProgressBar 
android:layout_width="wrap_content" 
android:id="@+id/progressBar1" 
android:layout_height="wrap_content"></ProgressBar>
Maneesh
  • 6,098
  • 5
  • 36
  • 55
0

It's hard to say, but try this for onClickListner:

button_search.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        new SearchTask().execute();
        setIs_loading(true);
        searchManager.startSearch(); 
    }
});

It's a pretty wild guess, but it might work

Vladimir
  • 9,683
  • 6
  • 36
  • 57
0

You don't need to create another task , just instead of doing the search stuff via another activity.

all you need to do is to put you search cod ein doInbackGround() of search task. e.g

@Override
protected Void doInBackground(Void... params) {

    global.getDataManager().doSearch();
    return null;
}

also use class level variable to get search result to store true/false

@Override
        protected void onPostExecute(Void ret){

            // Set you result here
            setIs_loading(my_searh_result);

            try {dismissDialog(DIALOG_LOADING_ID);} catch (Exception e){};

            if ( searchManager.errorOccurred() ){
                //Error
            } else {
                //No Error

            }   
        }
Arslan Anwar
  • 18,746
  • 19
  • 76
  • 105
  • Ok, instead of creating a second asynctask i put the network stuff in searchManager.startSearch(), and put the call form doInBackground but still the spinner doesnt turn – Camille R Nov 16 '11 at 12:35
  • 1
    Share your full doe, How do you showing spinner? Are you using a spinning dialog box? ProgressDialog ProgressIndicator = ProgressDialog.show(mContext, null , "Loading", true); – Arslan Anwar Nov 16 '11 at 12:38
  • Yep, you are right that was the problem ! dismiss deletes animation – Camille R Nov 16 '11 at 12:42