I have a Fragment and a Handler . In onCreateView I am trying to write 10,000 records to the database, while showing progress in ProgressDialog :

 class CreatePageFragment extends Fragment { ... ... ... private class ProgressHandler extends Handler { private ProgressDialog progressDialog; private int maxProgress; private Context mContext; ProgressHandler(Context c, int max) { mContext = c; maxProgress = max; progressDialog = new ProgressDialog(mContext); progressDialog.setTitle("Загрузка"); progressDialog.setMessage("Идет загрузка..."); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setMax(maxProgress); } public void handleMessage(Message msg) { if (progressDialog.getProgress() < progressDialog.getMax()) { progressDialog.incrementProgressBy(1); if (progressDialog.getProgress() == progressDialog.getMax()) { progressDialog.dismiss(); } } else { progressDialog.dismiss(); } } public void start() { progressDialog.show(); } } ... ... ... @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.first_table, container, false); final ProgressHandler handler = new ProgressHandler(getActivity(), 10000); handler.start(); Thread thread = new Thread(new Runnable() { public void run() { DataBase dataBase = new DataBase(getActivity(), myBase); SQLiteDatabase db = dataBase.getWritableDatabase(); ContentValues cv = new ContentValues(); for (int i = 0; i < 10000; i++) { cv.put("column_1", 1); cv.put("column_2", "text"); cv.put("column_3", "text"); db.insert("my_table", null, cv); cv.clear(); handler.sendEmptyMessage(0); } db.close(); dataBase.close(); } }); thread.start(); //Ожидание окончания загрузки synchronized (this) { try { thread.join(); } catch (Exception e) { } } return view; } ... ... ... } 

The problem is that I just have a black screen and the recording goes without dialogue, although the cycle is working (checked in the logs). Why doesn’t dialogue appear?

  • 2
    so what do you want to see? if you onCreateView , the UI will not be displayed until onCreateView - ermak0ff
  • @ bukashka101. Never write large amounts of data to the database “just in another stream”. It may happen that the process is interrupted in half of the action. There is a long time to explain such cases, just google on the topic "Why AsyncTask is outdated." I know that AsyncTask is not used here, but you will understand the meaning. - BORSHEVIK
  • one
    @BORSHEVIK, read. Then what to use for labor-intensive tasks? - user189127
  • @ bukashka101 The fact is that AsyncTask can also be used, but only in those cases where the end of the work flow is not important, as an example - these are situations when you upload pictures from the Internet to display on the screen, if they do not load, then figs with it, but in situations with your business it is much more serious, because if something in the database does not fall, then then I may have troubles. You can use IntentService, it is 100% guaranteed to make the work flow. - BORSHEVIK
  • @BORSHEVIK, then the sense of AsyncTask , if short tasks and the main thread can perform. - user189127

1 answer 1

As far as I understand your problem in calling the Join () method in the thread.join(); . It forces the main thread to wait for the work to work, so the View has not yet been prepared and you see a black screen