Back

Customizable confirmation dialog

Recently, while developing a project, the Powercode Android team has had to create dialogs. This topic is quite well covered in Dialogs, so you have a number of options:
  • Use AlertDialog directly inside its host, which in most cases is either target Fragment or Activity.
  • Extend DialogFragment and implement as if it is a standard Fragment.
  • Style Activity as Dialog with an appropriate theme, e.g. Theme.Holo.Dialog
  • Use third-party library with custom-made dialogs.
Every option has its own cons and pros. Third party libraries tend to bring additional dependencies and sometimes nasty bugs. Implementing every dialog, even the simplest one, with Activity might be an overkill, whereas extending DialogFragment is a good way though it may lead to a repetitive task especially if dialogs differ insignificantly. AlertDialog has a nice AlertDialog.Builder with many customizable options, but it does not save its state over configuration changes, such as device rotation. This forces a developer to somehow take case of saving/restoring dialog’s state and make sure it is dismissed to avoid memory leak. After the team had discussed possible solutions, we decided to combine DialogFragment and AlertDialog. Thus, the platform would handle saving/restoring state upon configuration change, and we still could enjoy flexibility of AlertDialog.Builder. Keep in mind that proposed solution puts some constraints on what/which and how you can customize a dialog. For instance, if you need to put a complex UI with components, it is better to make it as a separate fragment wired with particular layout. Returning to simple cases, the solution works quite well to display information/confirmation dialogs. Let’s break down an initial task into smaller sub-tasks.
  1. Give an option to choose what view-controller (Fragment or Activity) will interact with a dialog.
  2. Set up basic parameters such as theme, title, message, buttons with texts, etc. Some of these parameters are optional.
Dialog construction might be implemented via overloaded factory methods: code sample As you can see, for dialog parameters, where some of them are optional, we applied a Builder pattern: code sample Build() method assembles Bundle of fragment’s arguments. A straightforward way to interact with Fragment is via interface. In our case: code sample To alleviate a pain of having to implement interface completely, we can add default implementations, thanks to partial support of Java 8. To wire DialogFragment with another Fragment, a developer should use setTargetFragment() . For Activity cast context: code sample All required data is passed from the outside, and here is a dialog setup: code sample To set up the dialog, instantiate and pass parameters like this: code sample Target fragment use case: code sample Host Activity use case: code sample And finally: code sample The proposed solution is not intended to be a “Swiss Army Knife” for dialogs, but may save developer’s precious time and can be relatively easily adapted for your needs.

HAVE A PROJECT FOR US?

Let’s build your next product! Share your idea or request a free consultation from us.

Contact Us >