0

I am writing an very simple application with following scenario:

1) Screen A have 3 button to move on other screen's.

2) Now if I hold one button(say Button 1) and perform rapid click on other button then it launch multiple instance of other screen. Which I think should not be happened. How can prevent this.

3) and it's more weird. After move on other screen if I don't release Button 1 which was on Screen A then it still allow to perform click for rest of two button of screen A even I can see second screen.

Here it's clear launch second screen but still first screen button event working. Any idea how can avoid such scenario.

Renan Ferreira
  • 2,122
  • 3
  • 21
  • 30
CoDe
  • 11,056
  • 14
  • 90
  • 197
  • `it launch multiple instance of other screen. Which I think should not be happened. How can prevent this.` Please show your code ? – Code Geek Dec 29 '14 at 06:51
  • @Outofmemory Hardik..you can find this issue with most of standard application. I checked with FB_ and YahooNews_ app ..you just need to hold any one of button and just start to click any of other button..and keep on click even if you are on other page(Condition same..hold one button of screen). – CoDe Jan 03 '15 at 16:50
  • I don't think it should be something to bother with. Users that want to do gymnastic on a random app are a great minority (less than 0.1%?). It's not a matter of "if the user want to do this even though he shouldn't" anymore, it's just plain wrong. – Matthieu Harlé Jan 04 '15 at 10:34
  • Thanks @Shywim it could be but this should be handled ...Consider following scenario, 1) Working with Fragment based environment, and 2) have some button(to launch dialog for say) and list item to launch some other Activity then what happened dialog will get launch on next screen. – CoDe Jan 04 '15 at 12:02
  • @Shubh Am I right, you want to disable multitouch on your buttons? You can look for the possible solution here: http://stackoverflow.com/a/15641505/1533933 – krossovochkin Jan 04 '15 at 13:27
  • @krossovochkin this is not about multitouch..as I mentioned if user hold button of first screen and tab other button(jump to second activity)..then still you can get reference of click button on second screen as well(condition: keep hold position of first button). – CoDe Jan 05 '15 at 07:31

2 Answers2

0

One solution that I found is to disable click listener in onPause() and enable it in onResume() . Can we have better support for this?

chilljeet
  • 302
  • 2
  • 15
CoDe
  • 11,056
  • 14
  • 90
  • 197
0

How you are going to disable other buttons while having 1 enabled, that's an algorhytmic problem. You can try creating a boolean or control variable in your activity (and then pass the final reference of the activity to wherever you need it), or in a static context. But to answer the title of the question - you can "Cancel Touch Event" either by adding an OnTouchListener, or if you're extending class Button, you can override onTouchEvent(MotionEvent ev) method.

Using OnTouchListener will disable any previously defined touch-event behavior. You can call the actual click event from the inside by calling performClick method from your button.

//in order to use button inside OnTouchEvent, its reference must be final
//if it's not, create a new final reference to your button, like this:
final finalButton = button;

button.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        // ... additional code if necessary
        if(canBeClicked) {
            finalButton.performClick();
            return true;
        }
        else return false;
    }
}

Overriding onTouchEvent in a class extending Button should look something like this.

@Override
public boolean onTouchEvent(MotionEvent ev) {
    // ... additional code if necessary

    //here we don't really need to call performClick(), although API recommends it
    //we just send the touch event to the super-class and let it handle the situation.
    if(activity.canBeClicked) return super.onTouchEvent(ev);
    else return false;
}
cruelcore1
  • 578
  • 4
  • 22