A DialogFragment maintains an internal dialog and invokes the show and hide methods on it in accordance with its own life cycle. The call to FragmentTransaction.hide() simply tries to set the view of the fragment returned by Fragment.onCreateView() to View.GONE. The DialogFragment view is the same as the view used for the internal dialog, and therefore what you do hides the contents in the dialog box. Unfortunately, hiding the view does not "reject" the dialog box, so the screen will still be inaccessible.
When you call DialogFragment.show(FragmentTransaction,String) , a FragmentTransaction is created to add it to the FragmentManager . Typically, displaying a dialog is considered an “active” transaction, and then rejecting it simply pops the back stack an appropriate number of times. If you did not add any other fragments between them, then a new FragmentTransaction is created using the delete operation. If we could access this, we could just add an entry to the back and make this operation reversible. Unfortunately, this is not possible, so the best we can do is simply make our own method of dismissal (and hope that the internal state will not be too inflated):
public class UndoDialogFragmentActivity extends FragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) {
Clicking on the button in the fragment will open the DialogFragment dialog with its own dismissal button. After clicking the “Cancel” button, you can again display the dialog box by clicking the “Back” button, canceling the delete operation. This causes somewhat dubious behavior when you allow the reverse key to show and hide the dialog, but the details can be determined by you according to your application.
source share