-2

Because I often use the AlertDialog I would like seperate it to another class. I create method:

public static void showMessageDialog(Context context, int title, int message, DialogInterface.OnClickListener listenerPositive, DialogInterface.OnClickListener listenerNegative) {
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    if (title > 0) {
        builder.setTitle(title);
    }
    if (messge > 0) {
        builder.setMessage(messge);
    }
    if (listenerPositive != null) {
        builder.setPositiveButton(context.getString(R.string.button_ok), listenerPositive);
    }
    if (listenerNegative != null) {
        builder.setNegativeButton(context.getString(R.string.button_cancel), listenerNegative);
    }
    AlertDialog ad = builder.create();
    ad.show();
}

It's a good practise?

3
  • simply....sure...If You don´t want to do some further stuff inside the dialog, then this will save some work... Jan 20, 2016 at 12:39
  • Yes why not. Code optimization is good.
    – Rohit5k2
    Jan 20, 2016 at 12:40
  • I asked You because I don't know is the best solution of this method...
    – Pionas
    Jan 20, 2016 at 12:41

1 Answer 1

0

It's a good solution,I suggest you'd better place it in BaseActivity,which abstracts your method will be common used in activity,and let your other activity extends this BaseActivity.

4
  • Thanks, but I think it's too late to create BaseActivity in my project.
    – Pionas
    Jan 20, 2016 at 12:48
  • Placing it to a utility class will also be suitable.
    – starkshang
    Jan 20, 2016 at 12:52
  • Yes,you can do like this,I mean if you have other util methods for activity or fragment ,you can organize these(including this method) to a simple class.It will reduce your number of classes.
    – starkshang
    Jan 20, 2016 at 13:07
  • I woudl like to change my method to showMessageDialog(Context context, int title, int message, int button_ok, int button_cancel, DialogInterface.OnClickListener listenerPositive, DialogInterface.OnClickListener listenerNegative) and create another method for special action example: showMessageDialogPhoto(Context context, DialogInterface.OnClickListener listenerPositive, DialogInterface.OnClickListener listenerNegative) { showMessageDialogcontext, R.string.photo_title, R.string.photo_message, R.string.button_photo_ok, R.string.button_photo_cancel, listenerPositive, listenerNegative) }
    – Pionas
    Jan 20, 2016 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.