0

I repair big android app and I have many custom dialogs which inherit from various types of standard android dialogs (AlertDialog, ProgressDialog, ...).

I have to add option "setCanceledOnTouchOutside(false)" for all dialogs in app, because in ICS we have this option set on true by default android 4.0 Dialog gets canceled when touched outside of dialog window

I can add line "*dialog.setCanceledOnTouchOutside(false)" for every dialog in my project, but this is hard to maintanence solution.

I can't inherit from MyDialog which inherit from Dialog, because I inherit from AlertDialog, ProgressDialog,... too.

Probably the best solution would be set all dialogs option for whole project in one place or make any hack that give us by default behavior from older android version than ICS, but I don't know if it is possible and how to do this?

Can You advise me?

Community
  • 1
  • 1
jakub
  • 3,576
  • 3
  • 29
  • 55

1 Answers1

1

have all the dialogs implement a common interface, like this,

interface DialogSettings {
   void setCancelOnTouchOutside(boolean cancel);
}

now implement a stand-alone version of this,

class VersionAwareDialogSettings implements DialogSettings {
   private final Dialog d;

   DialogSettingsImpl(Dialog d) {
      this.d = d;
   }

   @Override
   void setCancelOnTouchOutside(boolean cancel) {
      if (Build.Version.SDK_INT ...) {
        d.setCancelOnTouchOutside(cancel);
      }
   }
}

now, in all your dialog classes, implement this interface, like this,

class MyDialog extends AlertDialog implement DialogSettings {
...
  @Override
  public void setCancelOnTouchOutside(boolean cancel) {
    new VersionAwareDialogSettings(this).setCancelOnTouchOutside(cancel);
  }
}

this can seem like a lof of cruft, but it's a nice separation of concerns. if you want to add a version-aware dialog setting, change the interface, and all parts of your code that you need to modify are flagged. if you need to change the impl of a verison-aware dialog setting, just change it in one place: VersionAwareDialogSettings.

you should be more concerned with writing correct code than trying to cut down on the number of lines you write. i don't know about you, but i can type much faster than i think. my brain is the bottleneck.

Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134
  • I need the same bahaviour on all Android versions: `setCancelOnTouchOutside(false)` but by default on ICS is `setCancelOnTouchOutside(true)` and on lower versions is `setCancelOnTouchOutside(false)` I think Your solution is great for different implementations dendendent on version, but for my case is too much complex, isn't it? – jakub Sep 13 '12 at 07:09