vovahost
vovahost

Reputation: 36069

How to dismiss a DialogFragment without removing it from FragmentManager

I want to hide a DialogFragment but also persist the state of all it's views and fields.

I tried using:

getSupportFragmentManager()
    .beginTransaction()
    .setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
    .hide(dialogFragment)
    .commit();

but it works not so well because it will hide the DialogFragment but will not hide the black transparent overlay behind the Dialog which I think is a Dialog Window property.
But then there is no way to show the DialogFragment using the same method because when I try this:

getSupportFragmentManager()
    .beginTransaction()
    .setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
    .show(dialogFragment)
    .commit();

it has no effect. The DialogFragment will not show.
Is there any other approach or should I manipulate the Dialog's View ?

Upvotes: 1

Views: 2131

Answers (1)

shuo Han
shuo Han

Reputation: 79

Use below to hide your dialog:

dialogFragment.getDialog().hide();

Use below to reshow your dialog:

dialogFragment.getDialog().show();

And I suggest you to judge if dialogFragment.getDialog() is null or not first when you call show() or hide()

Upvotes: 2

Related Questions