Faced a rather incomprehensible problem when testing the work of the ProgressDialog. The idea of ​​an example is that in a separate stream the value of the dialogue of progress increases. When ProgressDialog is called when the button is pressed, after its completion, Toast appears, which, despite the short display time, does not disappear at all. Restarting the dialog results in almost instantaneous execution and termination. Moreover, when exiting the application, the error "An unexpected application stop has occurred ... Please try again." How to resolve the situation?

Code overloaded thread 'a

package com.samples.progressdialog; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; public class SecondThread extends Thread{ Handler mHandler; final static int STATE_DONE = 0; final static int STATE_RUNNING = 0; int mState; int mTotal; SecondThread (Handler hnd) { mHandler = hnd; } public void run() { mState = STATE_RUNNING; mTotal = 0; while (mState == STATE_RUNNING) { try { Thread.sleep(100); } catch (InterruptedException e) { Log.e("ERROR", "THREAD INTERRUPTED"); } Message msg = mHandler.obtainMessage(); Bundle b = new Bundle(); b.putInt("Total", mTotal); msg.setData(b); mHandler.sendMessage(msg); mTotal++; } } public void setState (int state) { mState = state; } } 

Main file code

 package com.samples.progressdialog; import android.app.Activity; import android.app.ProgressDialog; import android.app.Dialog; import android.view.View; import android.view.View.OnClickListener; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.widget.Button; import android.widget.Toast; public class ProgressDialogActivity extends Activity { /** Called when the activity is first created. */ static final int IDD_PROGRESS = 0; private SecondThread mSecondThread; private ProgressDialog mProgressDialog; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final Button callButton = (Button)findViewById(R.id.button); callButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub showDialog(IDD_PROGRESS); } }); } protected Dialog onCreateDialog(int id) { switch(id) { case IDD_PROGRESS : mProgressDialog = new ProgressDialog(ProgressDialogActivity.this); mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); mProgressDialog.setMessage("Идёт загрузка чего-то там ... Пожалуйста ждите "); mSecondThread = new SecondThread(handler); mSecondThread.start(); return mProgressDialog; default: return null; } } final Handler handler = new Handler() { public void handleMessage(Message msg){ int total = msg.getData().getInt("Total"); mProgressDialog.setProgress(total); if (total >=100) { dismissDialog(IDD_PROGRESS); mSecondThread.setState(SecondThread.STATE_DONE); Toast.makeText(getApplicationContext(), "Task is finished", Toast.LENGTH_SHORT).show(); } } }; } 

    2 answers 2

    The problem with re-invoking the dialog is to cache the dialogs; when you call showDialog() , onCreateDialog() is called only once, on the first call. Subsequently, the dialogue is not destroyed, but simply gets from the "cache", but it calls onPrepareDialog() . Because the thread is launched in onCreateDialog() , then when it is called again, it simply does not start.

    You can try to run the stream in onPrepareDialog() , but I would recommend switching to AsynTask from thread , it’s easier to do there.

      Implemented the task using the recommended AsyncTask.

      Overloaded AsyncTask - ProgressTask.java

       package com.apps.myapptest.core; import android.os.AsyncTask; public class ProgressTask extends AsyncTask<Void, Integer, Boolean>{ private IProgressTracker mProgressTracker; private Integer Progress = 0; public ProgressTask (IProgressTracker progressTracker) { mProgressTracker = progressTracker; } @Override protected Boolean doInBackground(Void... arg0) { while (Progress <100) { if (isCancelled()) { return false; } try { Thread.sleep(100); publishProgress(Progress); } catch (InterruptedException e) { e.printStackTrace(); return false; } Progress++; } return true; } @Override protected void onProgressUpdate(Integer... progress) { mProgressTracker.onProgress(progress[0]); } @Override protected void onPostExecute(Boolean result) { if (result) mProgressTracker.onComplete(); else mProgressTracker.onCancel(); } } 

      MyAppTestActivity.java main activity file

       package com.apps.myapptest; import com.apps.myapptest.core.ProgressTask; import com.apps.myapptest.core.IProgressTracker; import android.app.Activity; import android.app.Dialog; import android.app.ProgressDialog; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; import android.os.Bundle; public class MyAppTestActivity extends Activity implements IProgressTracker{ /** Called when the activity is first created. */ private ProgressDialog mProgressDialog; private ProgressTask mAsyncTask; static final int IDD_PROGRESS = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button callButton = (Button) findViewById(R.id.buttonProgress); callButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub showDialog(IDD_PROGRESS); } }); } protected Dialog onCreateDialog(int id) { switch(id) { case IDD_PROGRESS: mProgressDialog = new ProgressDialog(this); mProgressDialog.setCancelable(true); //mProgressDialog.setIndeterminate(true); mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); mProgressDialog.setMessage("Ждите... Идёт загрузка"); mAsyncTask = new ProgressTask(this); mAsyncTask.execute(); return mProgressDialog; default: return null; } } @Override public void onProgress(Integer progress) { // TODO Auto-generated method stub if (!mProgressDialog.isShowing()) { mProgressDialog.show(); } if (progress <= 100) { mProgressDialog.setProgress(progress); } else { mProgressDialog.dismiss(); } } @Override public void onComplete() { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "Task is finished", Toast.LENGTH_SHORT).show(); mProgressDialog.dismiss(); } @Override public void onCancel() { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "Task is cancelled", Toast.LENGTH_SHORT).show(); mProgressDialog.dismiss(); } } 

      Interface IProgressTracker.java

       package com.apps.myapptest.core; public interface IProgressTracker { void onProgress(Integer progress); void onComplete(); void onCancel(); }