DoInBackground call AsyncTask then sleeps UI Thread blocks

I have an activity consisting of AsyncTask aimed at launching a request when a user clicks a button. I was looking for answers, but I did not find the same problem, or it is not the same for me. The code does what I want, but the ProgressDialog looks locked because the counter does not rotate sometimes (almost all the time).

When I click the button:

AsyncTask is launched β†’ showDialog () is called onPreExecute β†’ startSearch (SearchManager starts a new AsyncTask, and a heavy call to the network occurs in doInBackground) β†’ doInBackground in Activity is waiting for SearchManager β†’ to load.

Code for the button:

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

Code for AsyncTask in search operation:

 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 runs directly through startSearch

 protected class SearchAsync extends AsyncTask <Void,Void,Void>{ @Override protected Void doInBackground(ComSearchAds... datas) { global.getDataManager().doSearch(); //... When finished setIs_loading(false); } } 

I, apparently, am doing something wrong, but I can’t find what and how to avoid it. Thank you for your help!

DECISION:

Finally, it seems that the non-rotating ProgressDialog was because I used the same instance of the ProgressDialog and

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

used with a problem of reasons i changed to

 removeDialog(DIALOG_LOADING_ID) 

and now it works fine.

Thanks to everyone, and I hope that someday he will be able to help someone!

+4
source share
3 answers

You do not need to create another task, just instead of performing a search using another action.

all you have to do is put you in the cod ein doInbackGround () search task. eg,

 @Override protected Void doInBackground(Void... params) { global.getDataManager().doSearch(); return null; } 

also use class level variable to get search result to hold 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 } } 
0
source

Could you try to add the ProgressBar control to your layout file and set its Visible value to true when you run the async task and leave after the process is complete. Below is the code for it.

 <ProgressBar android:layout_width="wrap_content" android:id="@+id/progressBar1" android:layout_height="wrap_content"></ProgressBar> 
0
source

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(); } }); 

This is a pretty wild guess, but it may work.

0
source

Source: https://habr.com/ru/post/1381477/


All Articles