I am creating a fullscreen dialog like described here Showing a Dialog Fullscreen or as an Embedded Fragment.
So when I want to open this fullscreen dialog from my fragment I do:
FragmentTransaction transaction = getActivity()
.getSupportFragmentManager()
.beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.add(android.R.id.content, newFragment)
.addToBackStack(null)
.commit();
However there seems to be one problem with this. When I tap on the screen not only the dialog fragment receives touch events but the underlaying host fragment as well. So it happens that by tapping around an action is triggered in the host fragment.
Why does this happen? The host fragment should not receive touch events!?
I could solve this by not adding but replacing the host fragment with the dialog fragment but this will break the back stack.
So when I want to open this fullscreen dialog from my fragment I do
That has nothing to do with a dialog. The only way a DialogFragment
behaves like a dialog is if you call show()
.
Why does this happen?
You added a fragment to the same container as another existing fragment. Hence, both fragments' widgets are in the same view hierarchy as children of the same container -- you would see this in Hierarchy View, for example. There is no concept of "host fragment".
I could solve this by not adding but replacing the host fragment with the dialog fragment but this will break the back stack.
I wouldn't expect it to break the back stack. Pressing BACK should reverse the transaction and restore whatever fragment is in that container at the point when you commit()
-ted the transaction.