Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DialogFragment dismiss() does not pop backstack

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.

like image 450
Monster Hunter Avatar asked Mar 12 '15 05:03

Monster Hunter


People also ask

How do you dismiss a DialogFragment?

tl;dr: The correct way to close a DialogFragment is to use dismiss() directly on the DialogFragment. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog. Dismiss the fragment and its dialog.

Is DialogFragment deprecated?

This class was deprecated in API level 28. Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle.

How do you pass arguments to DialogFragment?

you can set your args. class IntervModifFragment : DialogFragment(), ModContract. View { companion object { fun newInstance( plom:String,type:String,position: Int):IntervModifFragment { val fragment =IntervModifFragment() val args = Bundle() args. putString( "1",plom) args.

What is the difference between dialog & DialogFragment?

DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs. It allows the FragmentManager to manage the state of the dialog and automatically restore the dialog when a configuration change occurs.


2 Answers

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!

like image 93
pablopatarca Avatar answered Oct 12 '22 12:10

pablopatarca


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.

like image 24
Delblanco Avatar answered Oct 12 '22 12:10

Delblanco