Guest User

Untitled

a guest
Dec 15th, 2011
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.26 KB | None | 0 0
  1. private static void showDialog(FragmentActivity activity, String title, String msg,
  2.         String positiveButtonText, String negativeButtonText, int id, Bundle args) {
  3.  
  4.     if (activity.isFinishing()) {
  5.         return;
  6.     }
  7.  
  8.     FragmentManager fmgr = activity.getSupportFragmentManager();
  9.     FragmentTransaction ft = fmgr.beginTransaction();
  10.     Fragment prev = fmgr.findFragmentByTag(TAG);
  11.     if (prev != null) {
  12.         try {
  13.             ft.remove(prev);
  14.         } catch (IllegalStateException ex) {
  15.             // issue: http://code.google.com/p/android/issues/detail?id=17029
  16.         }
  17.     }
  18.  
  19.     TwoButtonDialogFragment newFragment = new TwoButtonDialogFragment();
  20.     if (args == null) {
  21.         args = new Bundle();
  22.     }
  23.     args.putString("title", title);
  24.     args.putString("message", msg);
  25.     args.putString("positiveButtonText", positiveButtonText);
  26.     args.putString("negativeButtonText", negativeButtonText);
  27.     args.putInt("id", id);
  28.     newFragment.setArguments(args);
  29.     newFragment.setCancelable(false);
  30.     newFragment.show(fmgr, TAG); // exception is thrown here
  31.     ft.commit();
  32. }
  33.  
  34.  
  35. @Override
  36. public Dialog onCreateDialog(Bundle savedInstanceState) {
  37.     final Bundle args = getArguments();
  38.     String title = args.getString("title");
  39.     String msg = args.getString("message");
  40.     String positiveButtonText = args.getString("positiveButtonText");
  41.     String negativeButtonText = args.getString("negativeButtonText");
  42.     final int id = args.getInt("id");
  43.  
  44.     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  45.     if (!TextUtils.isEmpty(title)) {
  46.         builder.setTitle(title);
  47.     }
  48.     builder.setMessage(msg);
  49.  
  50.     final TwoButtonDialogHandler handler = (TwoButtonDialogHandler) getActivity();
  51.     builder.setPositiveButton(positiveButtonText, new DialogInterface.OnClickListener() {
  52.         public void onClick(DialogInterface dialog, int whichButton) {              
  53.             handler.doPositiveClick(id, args);
  54.         }
  55.     });
  56.     builder.setNegativeButton(negativeButtonText, new DialogInterface.OnClickListener() {
  57.         public void onClick(DialogInterface dialog, int whichButton) {
  58.             handler.doNegativeClick(id, args);
  59.         }
  60.     });
  61.  
  62.     return builder.create();
  63. }
Advertisement
Add Comment
Please, Sign In to add comment