0

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.

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?"

If the user presses Continue, progressDialog should continue the remaining work.

Otherwise, close the current activity.

CODE HERE:

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 >= 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;
    }
}

}

Jon O
  • 6,532
  • 1
  • 46
  • 57
chandu
  • 1
  • 1
  • 2
  • 5
    Your code is more like a "popup-hell" and it has significant flaws on a design level (e.g. back is not the only way to stop an activity). Do you want to address these, or do you want to get the code running (and be back tomorrow with your next problem)? – Michael Feb 07 '11 at 09:35
  • If anyone is still finding the solution, [try this one](http://stackoverflow.com/a/31737813/2128979). – Ashish Tanna Jul 31 '15 at 03:58

4 Answers4

3

Similar issue , When progressDialog.setCancelable(true) is set then hitting back button does not execute code written in back button code but just cancels the progress dialog..Hitting the key again works.

i want to cancel the progress dialog and execute some piece of code together which is not working. Clicking back button twice does not make sense to me.

3

You need to override back button event. You can do this as:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && progressDialog.isShowing()) {
        Log.d(this.getClass().getName(), "back button pressed");//write your own logic here, Whatever you want to do
    }
    return super.onKeyDown(keyCode, event);
}
Sandy
  • 6,285
  • 15
  • 65
  • 93
3

Well, I had the same issue. The simplest method that worked for me is using progressDialog.setCancelable(true).. This declares whether the dialog is cancelable by hitting the back key.. Try it and let me know if it works for you or not. Good luck

Farhan
  • 3,206
  • 14
  • 49
  • 62
2

You can use progressDialog.setCancelable(true) and then use progressDialog.setOnCancelListener() to provide the OnCancelListener to execute your code to be executed onCancel().

See the Android documentation on Dialogs.

Jon O
  • 6,532
  • 1
  • 46
  • 57