10
votes

I have a simple DialogFragment that calls dismiss when exits, according to the documentation:

public void dismiss()

Dismiss the fragment and its dialog. If the fragment was added to the back stack, all back stack state up to and including this entry will be popped. Otherwise, a new transaction will be committed to remove the fragment.

however, I found that the fragment is still on the backstack after calling dismiss() so I have to click back button to clear it. Does anyone know why ?

here's my code:

public void onCreate(Bundle b) {
        super.onCreate(b);
        setContentView(R.layout.test_layout);

        class MyDialogFragment extends DialogFragment implements OnClickListener{

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

                View v = inflater.inflate(R.layout.hello_world, container, false);

                Button b = (Button)v.findViewById(R.id.btn);
                b.setOnClickListener(this);

                return v;
            }

            @Override
            public void onClick(View v) {
                dismiss();
            }
        }

        getFragmentManager().beginTransaction().add(android.R.id.content, new MyDialogFragment(), "test").addToBackStack("b").commit();
    }

    @Override
    public void onBackPressed() {
        if (getFragmentManager().getBackStackEntryCount() > 0 ){
            getFragmentManager().popBackStack();
        } else {
            super.onBackPressed();
        }
    }
}

I also found out that if I don't override onBackPressed(), the back button simple doesn't work, no matter how many fragments I add to the activity, the back button always exits the activity right away.

2
I think the behavior from the docs happens when you add the fragment as an actual dialog, by using the show() methods, and not when treating it like a normal fragment like you do in your code. The BACK button should also work unless you're doing something fishy. - user
Are you using android.app.DialogFragment or android.support.v4.app.DialogFragment ? And did you find any solution for this issue ? - Ibrahim Disouki

2 Answers

7
votes

I can confirm what @Luksprog said in his comment: the dialog must be started through show(FragmentTransaction, String).

Note after looking the source: make sure to call addToBackStack(String) on the supplied transaction or else it still won't work.

1
votes

That it's a wrong way to create a DialogFragment.

Never ever use the FragmentManager to show a DialogFragment. To be shown there are a method called show(FragmentTransacion, String).

In java:

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

For another hand, to dismiss the dialog just do this:

mDialogFragment.dismiss()

Another think that I would like to highlight is that the MyDialogFragment class is defined inner onCreate method :'( Please, define the class outside the method or in another file if you want :)

Good Look!