Macarse
Macarse

Reputation: 93143

How Fragments handle touches?

I am playing with android's Fragments and I found something that didn't make a lot of sense to me. I am using the compatibility package and testing on a nexus one with 2.3.3 since I don't have a motorola xoom yet.

My issue is: When I replace a fragment with another, the one in the back continues to receive the touches.

Here's the code to reproduce.

I have a list of items and when you press a row a new fragment will be created and shown. BUT if you touch the green fragment, it will be received by the ListFragment, increasing the amount of back buttons press I have to make to go back to the ListFragment.

Can someone explain why?

EDIT: As CommonsWare suggested, I deleted the code pasted here since I opened this as an issue in the android issue tracker and you can download the demo project from there.

Upvotes: 11

Views: 4108

Answers (1)

bradley4
bradley4

Reputation: 3938

I had a similar problem. I have a smaller size fragment on top of a larger fragment where you can see the larger fragment sticking out from the smaller one. In some cases when I press the fragment on top it would trigger the onClick event handler of the fragment on the bottom.

To prevent this from happening I set the onClickListener to the parent layout of the smaller fragment. By doing this it prevents the onClick event from being passed to the fragment behind it.

LinearLayout ll= (LinearLayout) mView.findViewById(R.id.topLayout);
ll.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        //left empty on purpose to capture the onClick event. 
    }
});

Upvotes: 6

Related Questions