Adding a fragment on top of another fragment onClickListener issue

android, android-fragments

Solution

You can just add the following attribute to the XML root layout of the fragment that agoes on top-

android:clickable="true"

This will ensure that touch events will not propagate further than the top layer.

Problem

I'm adding a fragment to an activity instead of replacing the current fragment (because this corresponds to the type of behavior I want to have). My problem is that clicking in a spot on the top fragment (the one that's currently visible), where a view in the non-visible fragment is located, causes an onClick event on the view in the second, non-visible fragment, to fire. Why is this happening and how can I prevent this? This is the code I use to first add the ListView fragment to the activity: ``` @Override protected void onCreate(Bundle savedInstanceState) { ... if (savedInstanceState == null) { listFragment = new ListFragment (); getSupportFragmentManager().beginTransaction() .add(R.id.frame_container, listFragment) .addToBackStack(listFragment .TAG) .commit(); } ... } ``` In this same activity I'm adding the second fragment, on top of the list fragment: ``` @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { ... createItemFragment = new CreateItemFragment(); getSupportFragmentManager().beginTransaction() .add(R.id.frame_container, createItemFragment) .addToBackStack(createItemFragment.TAG) .commit(); ... } ```

Original source