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:
1-> Yes
2->No
3->Save and Exit
But the problem is when the home button is pressed NO alert dialog is being displayed.
I tried the following code:
// this is when the button is pressed
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;
The DiaryActivity is the main class.
DataView is the current activity.
The Yes button simply exits out of the current activity and goes back the main activity that is the DiaryActivity.
The No button "dismisses" the alert dialog and the user can then save his work and exit later.
The save and exit button save the work into the database and then exits out of the current activity to the main activity.