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?
onCreateView
, theUI
will not be displayed untilonCreateView
- ermak0ffAsyncTask
, if short tasks and the main thread can perform. - user189127