0

I have a fragment with touchable or clickable layout, i do not know why when i touch the fragment's layout and left my finger the funtion ""retrieveMQTTConnAssets()"" is caled twice "once when i touch the surface and keep touching and second time when i left my finger"

pease et meknow hw to avoid such behavior.

Update:

Actuay, I solved it now by instead of returning true, i returned false.but i do not understand it, why it is like this.

code:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Log.w(TAG, "@onCreateView()");
    View root = inflater.inflate(R.layout.eco_frag, container, false);
    root.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            //Toast.makeText(getActivity().getApplicationContext(), "toched", Toast.LENGTH_SHORT).show();
            retrieveMQTTConnAssets();
            return true;
        }
    });
    return root;
}
Ziem
  • 6,579
  • 8
  • 53
  • 86
Amrmsmb
  • 1
  • 27
  • 104
  • 226

1 Answers1

3

The problem is simple, and it is that

root.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        //this is called, right?
        retrieveMQTTConnAssets();
        return true;
    }
});

in onTouch, you receive a parameter called MotionEvent. That MotionEvent stores something called action, which can have many values (see reference), but most importantly ACTION_DOWN and ACTION_UP.

If you want to make it so that it runs only when you release it, then you should check for that type of action.

@Override
public boolean onTouch(View v, MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_UP) {
        retrieveMQTTConnAssets();
    }
    return true;
}
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • 1
    I came to say that. To explain a bit more: `ACTION_DOWN` is when the user starts touching the screen and `ACTION_UP` is when the user stops touching the screen – josebama Apr 28 '15 at 11:29