[ACCEPTED]-Android : Dialog: should I hide or dismiss-dialog

Accepted answer
Score: 65

Using hide() could cause a Leaked Window error.

If you choose to use hide() and you exit your 4 application using finish(), this will cause an error 3 message (seen here) about a window being leaked.

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

Score: 28

It depends on how many time your need it, and 6 if it is expensive to create it. If it is 5 not too expensive to create it, I would 4 personally prefer to dismiss it, to have 3 a "cleaner environment". But if you're not 2 using hundreds of dialogs, I don't think 1 this really matters.

Score: 16

I know that It is a very old post but I've 5 found none of the answers above good enough, so 4 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 3 you need to show and hide a dialog many 2 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 1 it will be useful for feature references.

Score: 1

I assume by 'static' you mean the content 20 is not dynamic, not that you've got static 19 objects in your code. In that case it's 18 probably best to dismiss the dialog and 17 allow the VM to recollect any memory allocated 16 for it. The resources necessary to create 15 a dialog are trivial but holding onto memory 14 when it's not very frequently used is a 13 good way to starve the system of memory.

Consider 12 your app may be one of half a dozen apps 11 running. If they all kept their "cheap" objects 10 hidden instead of dismissing them pretty 9 soon something's going to be forced to close 8 by the VM to reclaim memory.

At the same 7 time we're talking about a Dialog which 6 is not exactly a large object. I'd offer 5 the standard behavior would be to dismiss 4 it unless you can create a convincing argument 3 why it's cheaper to hide it to save resources 2 on re-creation (for example, if you're very 1 frequently displaying this dialog).

More Related questions