Raigex
Raigex

Reputation: 1215

Why is my back button not working right after dismissing dialog fragment

I create a dialog fragment and then when I press back it is dismissed. Unfortunately, when I press the back button again nothing happens (what is supposed to happen is I am supposed to leave this activity and go to previous one). How can that be? When I dismiss the fragment (using either dismiss() or back button) it should not be catching any more of my back button presses (and I am unsure if it does catch that event).

Can anyone say what the error might be?

Code below

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

    Fragment prev = getSupportFragmentManager().findFragmentByTag(
            "File Browser Fragmnent");
    if (prev != null) {
        ft.remove(prev);
    }
    ft.addToBackStack(null);
    FileBrowseDialog dialog = FileBrowseDialog.newInstance(currentVideoId);
    if (dialog.getDialog() != null)
        dialog.getDialog().setCanceledOnTouchOutside(true);
    dialog.setCancelable(true);

    if(darkenedOverlay != null && darkenedOverlay.getVisibility() != View.VISIBLE)
        darkenedOverlay.setVisibility(View.VISIBLE);
    ft.add(dialog, "File Browser Fragmnent");
    ft.show(dialog).commit();

Then when I press the back button the dialog fragment disappears (I am unsure if it is just hiding or what) Also this only happens when using ft.add() and ft.show().commit();

If I use below code instead of ft.add().show().commit() it works with no problems.

dialog.show(ft, "File Browser Fragment");

Hopefully someone here can help, and thanks in advance.

Upvotes: 2

Views: 699

Answers (1)

Raigex
Raigex

Reputation: 1215

I found why that happens. It happens because I use addToBackstack which basically undo's the last transaction (the creation of the dialog) . If i remove addToBackstack i no longer have a problem with the back button.

Upvotes: 1

Related Questions