An invisible layout behind the fragment is getting clicked:
Setting an onClickListener on VenueFragment will work, but I'll add a little bit more explanation. Having the main fragment get a touch event is intended behavior to handle the case where your venue fragment is transparent. In that case, it could be reasonable to expect the touch to go through your transparent view to the one underneath. As such, android passes touch events until they can be handled. Setting an onClickListener will handle the event, preventing it from being passed to the next View. If you wanted to be "correct" about preventing clicks on the lower fragment, there are a couple of options:
- Remove the onClickListener from the main fragment in OnPause, and set it in OnResume. That way, any time the main fragment is not the active fragment, it cannot be clicked.
- Subclass your top level layout in your VenueFragment and override OnTouchEvent to return true. This will essentially do the same thing as setting an onClickListener, but you may find that it makes your intention to block all touch events clearer.
Well the very quick answer is android:clickable="true"
in the top of the layout properties in fragment.
But it would be a better practice if you create an activity with a FrameLayout & its id and no other elements in that activity. So whenever you want a fragment to be on the screen, give a call to that activity and use
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right)
.replace(R.id.frameLayoutId, mainFragment, MAIN_FRAGMENT_TAG)
.commit();
use replace instead of add so that the frameLayout of the activity is replaced by the fragment's layout as given by the frameLayoutId
Set clickable property on the second fragment's view to true.
For Example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
You need to make the root ViewGroup
of VenueFragment
clickable so it handles click events and they do not pass down (in the z-order sense) to the other Fragment
.