Best way to implement multiple Alert Dialogs in an Activity
android, android-alertdialog, android-fragments
Solution
Modern approach to creating `AlertDialog`s is actually to wrap them inside `DialogFragment`s, so you need to create a class extending `DialogFragment`, override its `onCreateDialog()` method and return an instance of `AlertDialog` from it. For the purpose of customizing the dialogs you can add a `newInstance()` method to your `DialogFragment` as described in this discussion and pass custom parameters to your class.
Problem
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.