AlertDialog not working
<p>Guys I have an activity which has a home button, when the button is pressed,an alert dialog should appear with the message "Exit without Saving ?"
and the following options(buttons) should be available to the user:</p>

<p>1-> Yes</p>

<p>2->No</p>

<p>3->Save and Exit</p>

<p>But the problem is when the home button is pressed NO alert dialog is being displayed.</p>

<p>I tried the following code:</p>

<p>// this is when the button is pressed</p>

<pre><code>public void onClick(View v) {
    // TODO Auto-generated method stub
switch (v.getId()) {

case R.id.backHome:
        final AlertDialog alertDialog = new AlertDialog.Builder(
                DataView.this).create();
        // alertDialog.setTitle("Exit Without Save ?");
        alertDialog.setMessage("Exit Without Saving");
        alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Intent i = new Intent(DataView.this, DiaryActivity.class);
                startActivity(i);
                finish();

            }
        });

        alertDialog.setButton2("No", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                alertDialog.dismiss();

            }
        });
        alertDialog.setButton3("Save and Exit",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        rowIdreceived = getdata.getLong("row_id");
                        String title_updated = topicDisplay.getText()
                                .toString() + " ";
                        String story_updated = StoryField.getText()
                                .toString();
                        DataHolder entry = new DataHolder(DataView.this);

                        try {
                            entry.open();
                        } catch (SQLException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        entry.updateEntry(rowIdreceived, title_updated,
                                story_updated);

                        entry.close();
                        Intent i = new Intent(DataView.this,
                                DiaryActivity.class);
                        startActivity(i);
                        finish();

                    }
                });


        break;
</code></pre>

<p>The DiaryActivity is the main class.</p>

<p>DataView is the current activity.</p>

<p>The Yes button simply exits out of the current activity and goes back the main activity that is the DiaryActivity.</p>

<p>The No button "dismisses" the alert dialog and the user can then save his work and exit later.</p>

<p>The save and exit button save the work into the database and then exits out of the current activity to the main activity.</p>
