I’m playing around with Dialog to create some quick views in my app (like login enter name etc)
and I’m wandering what is better: hide or dismiss.
I know what they both do but I keep wandering if it is better to just hide a Dialog and show it again when I need or to dismiss it and recreate it.
my Dialogs are small and are acutally static in my code so as such I don’t hold tons of instances.
So can somebody give me the pros and cons of using hide over dismiss.
It depends on how many time your need it, and if it is expensive to create it. If it is not too expensive to create it, I would personally prefer to dismiss it, to have a “cleaner environment”. But if you’re not using hundreds of dialogs, I don’t think this really matters.
hide() could cause a
Leaked Window error.
If you choose to use
hide() and you exit your application using
finish(), this will cause an error message (seen here) about a window being leaked.
dismiss() your dialogs properly before calling
finish() or just use
dismiss() instead of
I know that It is a very old post but I’ve found none of the answers above good enough, so in the simplest way of explaining:
hide()will just change the visibility status of the dialog but the object will be still there and can be shown again using
dismiss()hides and also destroys the dialog. To show the dialog again it needs to be recreated first.
Then if you need to show and hide a dialog many times better to
hide() it. eventually
dismiss() it on
onDestroy() to avoid the window leak error. Note that leaving the activity when a dialog not dismissed causes the memory leak.
hope it will be useful for feature references.
I assume by ‘static’ you mean the content is not dynamic, not that you’ve got static objects in your code. In that case it’s probably best to dismiss the dialog and allow the VM to recollect any memory allocated for it. The resources necessary to create a dialog are trivial but holding onto memory when it’s not very frequently used is a good way to starve the system of memory.
Consider your app may be one of half a dozen apps running. If they all kept their “cheap” objects hidden instead of dismissing them pretty soon something’s going to be forced to close by the VM to reclaim memory.
At the same time we’re talking about a Dialog which is not exactly a large object. I’d offer the standard behavior would be to dismiss it unless you can create a convincing argument why it’s cheaper to hide it to save resources on re-creation (for example, if you’re very frequently displaying this dialog).