How to cancel alert dialog box and the activity on back pressed
<p>I have an alert dialog box in my application for authentication. When i pressed the back button in the emulator the alert dialog gets disappear but the activity still running. Now i need to cancel the alert box and both the activity on back pressed and want to go to the mobile main screen. Iam using the following code:<br></p>

<pre><code> @Override
        public void onStart() {
            super.onStart();        
            final SharedPreferences prefs = PreferenceManager
                    .getDefaultSharedPreferences(getApplicationContext());
            logutils.createlog();
            if (prefs.getString(getResources().getString(R.string.token), null) != null) {
                setContentView(R.layout.mainscreen);
                showButtons();
            } else {
                createDB();
                status = "completed";
                SharedPreferences.Editor prefsEditor = prefs.edit();
                prefsEditor.putString("status", status);
                prefsEditor.commit();
                final AlertDialog.Builder alert = new AlertDialog.Builder(this);
                LinearLayout login = new LinearLayout(this);
                TextView tvUserName = new TextView(this);
                TextView tvPassword = new TextView(this);
                TextView tvURL = new TextView(this);
                final EditText etUserName = new EditText(this);
                final EditText etPassword = new EditText(this);
                final EditText etURL = new EditText(this);
                if (dbhostname == null) {
                    etURL.setText(null);
                } else {
                    etURL.setText(dbhostname);
                }
                login.setOrientation(1); // 1 is for vertical orientation
                tvUserName.setText(getResources().getString(R.string.username));
                tvPassword.setText(getResources().getString(R.string.password));
                tvURL.setText("SiteURL");
                login.addView(tvURL);
                login.addView(etURL);
                login.addView(tvUserName);
                login.addView(etUserName);
                login.addView(tvPassword);
                etPassword.setInputType(InputType.TYPE_CLASS_TEXT
                        | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                login.addView(etPassword);
                alert.setView(login);
                alert.setTitle(getResources().getString(R.string.login));
                alert.setCancelable(true);
                alert.setPositiveButton(getResources().getString(R.string.login),
                        new DialogInterface.OnClickListener() {
                            public void onClick(final DialogInterface dialog,
                                    int whichButton) {                          
                                strhwdXml = etURL.getText().toString();
                                strUserName = etUserName.getText().toString();
                                XmlUtil.username = strUserName;
                                strPassword = etPassword.getText().toString();
                                if ((strUserName.length() == 0)
                                        &amp;&amp; (strPassword.length() == 0)
                                        &amp;&amp; (strhwdXml.length() == 0)) {
                                    Toast.makeText(
                                            getBaseContext(),
                                            getResources().getString(
                                                    R.string.userPassword),
                                            Toast.LENGTH_SHORT).show();
                                    onStart();
                                } else {
                                    new doprogress().execute();
                                    // doAuthentication();
                                }                           
                            }
                        });
                alert.setNegativeButton(getResources().getString(R.string.exit),
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {
                                dialog.cancel();
                                System.exit(0);
                            }
                        }).show();          
                alert.setOnCancelListener(new OnCancelListener() {
                    public void onCancel(DialogInterface dialog) {
                        // TODO Auto-generated method stub
                        finish();
                        System.exit(0);
                    }              
                });
            }
        }
</code></pre>
