Best way to implement multiple Alert Dialogs in an Activity

Viewed 12278
  • I have an Activity with 3 Fragments, now each fragment has 2 ToggleButtons(total six buttons).

  • An AlertDialog is shown to the user on each button click.

  • Each button does some different action and so the layouts/views of Alert Dialog differ from each other, and so the positive button click for dialogs too differ, while the negative button click are almost same.

I have implemented the logic in an onButtonPressed method in the Activity as

onButtonPressed(View v){

    switch(v.getId()){
    case R.id.button1:
        // create and show an AlertDialog
    break;
    }
    case R.id.button2:
        // create and show an AlertDialog
    break;
    }
    case R.id.button3:
        // create and show an AlertDialog
    break;
    }
    .
    .
    .
}

This leads to lots of repetitive lines of code, which isn't the best thing AFAIK. I was willing to know if I should keep current implemention, or create a wrapper class something for creating and showing AlertDialogs.

3 Answers
Related