Show confirmation dialog in snippet

I am converting my Android application to use fragments. I used to have activity, which is now a fragment. Therefore, this code can no longer be used:

showDialog(CONFIRM_ID);
// ...
@Override
public Dialog onCreateDialog(int id) {
    // Create the confirmation dialog...
}

Inside the object, FragmentI need to display a confirmation dialog box, which, after confirmation, returns me an object to update the status.

eg.

  • Inner fragment X.
  • Show confirmation dialog.
  • If yes updates the UI for X.

How can i do this? Please provide a working code example.

+5
source share
2 answers

(AlertDialog), , : http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
       .setCancelable(false)
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                MyActivity.this.finish();
           }
       })
       .setNegativeButton("No", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
           }
       });
AlertDialog alert = builder.create();
alert.show();

, , :

+9
         AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());       
         builder.setTitle("Your Title");
         builder.setMessage("Your Dialog Message");
         builder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int id) {
                  //TODO
                  dialog.dismiss();
             }
         });
         builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int id) {
                  //TODO
                  dialog.dismiss();
             }
         });
         AlertDialog dialog = builder.create();
         dialog.show();
0

All Articles