Reputation: 3844
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?
Upvotes: 0
Views: 234
Reputation: 22637
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.
Upvotes: 1