Android: FragmentTransaction hide does not work for DialogFragment

I'm trying to add

fragmentTransaction.hide(myDialogFragment); fragmentTransaction.addToBackStack(null); 

in FragmentTransaction so that the dialog box reappears when the user clicks the back button, but it doesn’t work. I initially redefined onCreateDialog in my DialogFragment, but I noticed that the documentation for hide triggers the FragmentTransaction state:

This only applies to fragments whose views have been added to the container.

So now I am redefining onCreateView. Now this is a kind of skins, but in fact. The dialog simply shrinks, but the window still remains dark. I have to press the back button to get rid of it, and that’s not what I want. What am I missing here?

+4
source share
3 answers

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) { // show a dialog fragment in the normal way new MyDialogFragment().show(getSupportFragmentManager(), "dialog"); } }); } private static class MyDialogFragment extends DialogFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setStyle(STYLE_NO_TITLE, getTheme()); // do not allow back button to dismiss dialog; confusing behaviour otherwise! setCancelable(false); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Button button = new Button(getActivity()); button.setText("Dismiss"); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // pressing back after 'dismissing' the dialog will cause it to be added again getFragmentManager().beginTransaction().remove(MyDialogFragment.this).addToBackStack(null).commit(); } }); return button; } } } 

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.

+8
source

I managed to hide the DialogFragment dialog by calling getDialog().hide() from my DialogFragment dialog.

+4
source

If you use API Level 11 or higher, you can simply call dismiss() on the DialogFragment , either from the FragmentActivity or from DialogFragment .

+1
source

Source: https://habr.com/ru/post/1395327/


All Articles