Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 903
Macarse Avatar asked Mar 15 '11 21:03

Macarse


People also ask

Which method you should override to control your touch action?

To make sure that each view correctly receives the touch events intended for it, override the onInterceptTouchEvent() method.

What is the difference between fragment and activity?

Activity is an application component that gives a user interface where the user can interact. The fragment is only part of an activity, it basically contributes its UI to that activity. Fragment is dependent on activity. It can't exist independently.

Why fragment is used in Android?

According to the Android documentation, a fragment is a part of applications user interface that is bound to an activity. Fragments have their lifecycle and layouts or UI components. Fragments help enrich your UI design, pass data between different screens, and adapt to different device configurations.


1 Answers

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. 
    }
});
like image 191
bradley4 Avatar answered Oct 28 '22 01:10

bradley4