Note that there are some explanatory texts on larger screens.

plurals
  1. POBACK Button is not working ,while progressDialog is running
    primarykey
    data
    text
    <p>I have a little problem; I hope you can help me. While progressDialog is running, the user presses the BACK Button. The current Activity progressDialog goes to Background, but progressDialog is running.</p> <p>My problem is that when the user clicks on the BACK button, progressDialog should be foreground Activity, stop current progress and ask "Do you want to continue or exit?"</p> <p>If the user presses Continue, progressDialog should continue the remaining work.</p> <p>Otherwise, close the current activity.</p> <p>CODE HERE:</p> <pre><code>public class SampleExample extends Activity { static final int PROGRESS_DIALOG = 0; Button button; TextView download; ProgressThread progressThread; ProgressDialog progressDialog; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); download = (TextView) findViewById(R.id.download); button = (Button) findViewById(R.id.progressDialog); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { showDialog(PROGRESS_DIALOG); } }); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // Handle the back button if (keyCode == KeyEvent.KEYCODE_BACK) { // Ask the user if they want to quit new AlertDialog.Builder(this).setIcon( android.R.drawable.ic_dialog_alert).setTitle("Exit") .setMessage("Are you sure you want to leave?") .setNegativeButton(android.R.string.cancel, null) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // Exit the activity SampleExample.this.finish(); } }).show(); // Say that we've consumed the event return true; } return super.onKeyDown(keyCode, event); } protected Dialog onCreateDialog(int id) { switch (id) { case PROGRESS_DIALOG: progressDialog = new ProgressDialog(SampleExample.this); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setMessage("Loading..."); download.setText("Now downloading......."); progressThread = new ProgressThread(handler); progressThread.start(); progressDialog.setCancelable(true); return progressDialog; default: return null; } } // Define the Handler that receives messages from the thread and update the // progress final Handler handler = new Handler() { public void handleMessage(Message msg) { int total = msg.getData().getInt("total"); progressDialog.setProgress(total); if (total &gt;= 100) { download.setText(" download is completed."); dismissDialog(PROGRESS_DIALOG); progressThread.setState(ProgressThread.STATE_DONE); } } }; /** Nested class that performs progress calculations (counting) */ private class ProgressThread extends Thread { Handler mHandler; final static int STATE_DONE = 0; final static int STATE_RUNNING = 1; int mState; int total; ProgressThread(Handler h) { mHandler = h; } public void run() { mState = STATE_RUNNING; total = 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", total); msg.setData(b); mHandler.sendMessage(msg); total++; } Log.d("SampleExample", "6666 run () 6666 End"); } /* * sets the current state for the thread, used to stop the thread */ public void setState(int state) { mState = state; } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload