I have a class inheriting from AsyncTask :

 class LoadData extends AsyncTask<Void, Void, Integer> { private Context mContext; private String baseName; private DataBase dataBase; private SQLiteDatabase db; private ProgressDialog progressDialog; LoadData(Context context, String name) { mContext = context; baseName = name; progressDialog = new ProgressDialog(mContext); progressDialog.setTitle("Загрузка"); progressDialog.setMessage("Идет загрузка..."); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setIndeterminate(true); } @Override protected void onPreExecute() { super.onPreExecute(); progressDialog.show(); } @Override protected Integer doInBackground(Void... params) { for (int i = 0; i < 10000; i++) { dataBase = new DataBase(mContext, baseName); db = dataBase.getWritableDatabase(); ContentValues cv = new ContentValues(); cv.put("text_1", "text 1"); cv.put("text_2", "text 2"); cv.put("text_3", "text 3"); cv.put("text_4", "text 4"); cv.put("text_5", "text 5"); db.insert("text", null, cv); db.close(); dataBase.close(); } return 0; } @Override protected void onPostExecute(Integer result) { super.onPostExecute(result); progressDialog.dismiss(); } } 

I call it in activity and wait for it to finish like this (after calling setContentView ):

 LoadData loadData = new LoadData(this, "myBase"); loadData.execute(); try { int num = loadData.get(); } catch (Exception e) {} 

The problem is that the dialogue does not appear. Just a black screen, but in the log you can see that the stream is working!

How to make this dialogue appear, and the activity to wait for its completion?

  • 3
    Why do we need to output the operation to a separate thread, then to slow down the main thread, waiting for the execution of a separate thread? The meaning of a separate thread is that the main thread should not WAIT for its completion. - pavlofff
  • @pavlofff, and I was ready for this question! : D To, when you rotate the screen, recover AsyncTask with all the data loaded. If it is half full, it will be half restored, completely full, I will restore it completely and immediately get the result. And how to do it in Activity I do not know. Yes, and I tried to do the same in activity, but it hung and its dialogue did not even have time to appear. - user189127
  • Redefining the methods of working with a UI stream inside AsyncTask itself AsyncTask meaningless - it does not have access to the UI and, accordingly, will not display anything, as you have already noticed. Thus, once again you come up with a self-made crutch and ask questions about it, instead of solving the real problem - how to maintain the state of the asynchronous while turning the activation. - pavlofff
  • @pavlofff, how to save the state of AsyncTask'a , when I rotate the screen, I already understood, but how to make it show the dialogue ... Now I’ll go try the @Saidolim option. - user189127

1 answer 1

Create a ProgressDialog in an Activity and change its parameters from AsyncTask .

In LoadData create a constructor and transfer the activity. as a matter of fact

 private MyActivity myActivity; public LoadData (MyActivity myActivity){ this.myActivity = myActivity; } @Override protected void onPreExecute() { super.onPreExecute(); this.myActivity.progressDialogShow(true); } @Override protected void onPostExecute(Integer result) { super.onPostExecute(result); this.myActivity.progressDialogShow(false); } 

an idea like that.

And in the Activity need a function

 public void progressDialogShow(boolean isShow){ // Если isShow = true нужно показать ProgressBar // else Отключит его } 

You can also show% values ​​when working through a function.

 onProgressUpdate(Progress...) 

UPD: from comments

Calling get to the main activity creates a black screen, as it waits for AsyncTask complete.

UPD2:

After searching the comment, updated the answer

  • The result is the same ... - user189127
  • @ bukashka101, this answer is completely correct. We just need to mention that calling .get(); above task you start to wait for its execution in the main thread. What completely kills with extreme cruelty is the very meaning of the existence of dachshunds as such. - Yuriy SPb
  • @Yuriy SPb, I already wrote pavlofff, why did I do that. Comments are questionable. - user189127
  • 2
    @Saidolimm in onCreate I call. In the activity itself. And then I create AsyncTask . And add to the answer that get creates a black screen. - user189127
  • one
    You are not right. Tomorrow the progressdialog will be replaced by a progressbar and you will have to change two classes. And / or create a dialogfragment and get problems with the life cycle of the fragments ... As a result, it all turns into try{..}catch(Exception e){} , as in the code from the question. - Yura Ivanov