36

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 actually 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.

SMR
  • 6,628
  • 2
  • 35
  • 56
Jason Rogers
  • 19,194
  • 27
  • 79
  • 112
  • 2
    An aside here: Static references to Dialogs are a bad idea, just as they are with any other Views. It's a memory leak waiting to happen. See http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html – Yoni Samlan Apr 01 '11 at 19:24

4 Answers4

66

Using 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.

So, either dismiss() your dialogs properly before calling finish() or just use dismiss() instead of hide().

Community
  • 1
  • 1
Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
28

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.

Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
RoflcoptrException
  • 51,941
  • 35
  • 152
  • 200
17

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 show() method.
  • 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.

Masoud Dadashi
  • 1,044
  • 10
  • 11
1

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).

BrionS
  • 283
  • 3
  • 15