Question

I have an application that is using fragments. The set up is like so:
Main Activity loads, loads fragment activity into right portion of parent activity -From the Fragment, I launch a DialogFragment which displays a list of users -From the DialogFragment, if you click on one of the users in the list, it hides the list of users DialogFragment .hide() and shows a new DialogFragment containing the details about the user

This all works great. However, when I click the Close button on the Details DialogFragment, I'd like to dismiss() that dialog, and re-show the List of Users dialog.

I realize this is somewhat difficult to follow. Does anyone have any insight that may help me?

UPDATE

The code I use to display the DialogFragment is the following:

MyDialogFragment dialog = new MyDialogFragment();
dialog.show(getFragmentManager(), "MyDialogFragment");

Then once in the dialog fragment, if I wanted to hide it and show the details fragment I call

dialog.hide();
MyDetailsFragment details = new MyDetailsFragment();
details.show(getFragmentManager(), "MyDetailsFragment");

Basically I need to be able to re-show the dialog above when I dismiss the details.

Was it helpful?

Solution

When a fragment transaction is performed, you can add it to the back stack which can be reversed on dismissing the dialog.

Begin a fragment transaction and use the DialogFragment.show(FragmentTransaction transaction, String tag) variant which takes the FragmentTransaction as parameter. It will take care of showing the Dialog, adding the fragment to the passed transaction and then committing the transaction. Later when the Dialog is dismissed, DialogFragment will itself take care of popping the transaction.

You can follow the first sample posted in the DialogFragment docs.

Here is the working code:

   public void launchMyDialog(View v) {
        // DialogFragment.show() will take care of adding the fragment
        // in a transaction.  We also want to remove any currently showing
        // dialog, so make our own transaction and take care of that here.
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        Fragment prev = getFragmentManager().findFragmentByTag("mydialog");
        if (prev != null) {
            ft.remove(prev);
        }
        ft.addToBackStack(null);

        MyDialogFragment dialog = new MyDialogFragment();
        dialog.show(ft, "mydialog");
    }

    public static class MyDialogFragment extends DialogFragment {

        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.fragment_main, container, false);
            Button b = (Button) v.findViewById(R.id.button);
            b.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    FragmentTransaction ft = getFragmentManager().beginTransaction();
                    Fragment prev = getFragmentManager().findFragmentByTag("mydialog");
                    if (prev != null) {
                        ft.remove(prev);
                    }
                    ft.addToBackStack(null);

                    MyDetailsFragment dialog = new MyDetailsFragment();
                    dialog.show(ft, "mydialog");

                }

            });
            return v;
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top