Fixed BadTokenException when we try to open a dialog when the activity is already closed and some other improvements with dialogs