0

I have an Activity that contains four Buttons that all trigger the same onClick() method when clicked. I want to prevent concurrent calling/access of that method.

  1. When button1 is clicked, the onClick() method causes a scrolling horizontal Scrollview to scroll a few pixels and display another view.
  2. This newly displayed View has a back button to scroll back to the previous position. [Things are working as expected up to this point].

Problem:

If in the Activity two buttons are pressed simultaneously (or only a few milliseconds apart), then my custom scroll method calls again before completing first button click execution.

Tried Case:

  1. Using the synchronized keyword with the method, which is called from the button's onClick with UI thread. [Not working]
  2. Tried setting "static boolean flag," hasClicked to true during onClick() execution, and checking it immediately inside onClick(), returning if true. [Not working]

Related Code:

    private OnClickListener mClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        int viewId = view.getId();

        //Proshop top icon
        if(viewId == R.id.proshop_icon){
            if(hasClicked){
                return;
            }
            hasClicked = true;
            mProShopIconTop.startAnimation(proShopButtonAnimation());
            if(mHScrollView.getCurrentChildIndex() == 0){
                addAndMoveToSecondPage(mProshopPageView, true);
            } else {
                addAndMoveToSecondPage(mProshopPageView, false);
            }
            hasClicked = false;
        }
        //Tune top icon
        else if(viewId == mTuneIconTop.getId()){
            if(hasClicked){
                return;
            }
            hasClicked = true;
            mTuneIconTop.startAnimation(tuneButtonAnimation());
            if(mHScrollView.getCurrentChildIndex() == 0){
                addAndMoveToSecondPage(mTunePageView, true);
            } else {
                addAndMoveToSecondPage(mTunePageView, false);
            }
            hasClicked = false;
        }
        //bottom and top aiConnect button
        else if(viewId == mAiConnect.getId() || viewId == mAiConnectIconTop.getId()){
            if(hasClicked){
                return;
            }
            hasClicked = true;
            mAiConnectIconTop.startAnimation(aiConnectButtonAnimation());
            if(mHScrollView.getCurrentChildIndex() == 0){
                addAndMoveToSecondPage(mAIConnectPageView, true);
            } else {
                addAndMoveToSecondPage(mAIConnectPageView, false);
            }
            hasClicked = false;
        }
        //DR page play game button
        else if(viewId == mPlayGame.getId()){
            if(hasClicked){
                return;
            }
            hasClicked = true;
            playGolfClick();
            addAndMoveToSecondPage(mGameMenuPageView, true);
            hasClicked = false;
        }
    }
}

This is code which I called from onCLick.

private void addAndMoveToSecondPage(View view, boolean withScroll){
    mHScrollView.removePage(1);
    mHScrollView.addPage(view, 1);
    if(withScroll){
        mHScrollView.moveToNextView();
    }
    view.setVisibility(View.VISIBLE);
}

mHScrollView is the instance of HorizontalScrollView which is customized by me, I incorporated the method below in my CustomHorizontalScrolView which is a sub-class of HorizontalScrolView.

public void moveToNextView(){
    smoothScrollTo(getScrollX() + mScreenWidth, 0);
}

Any help will be appreciated.. Thanks in advance..!!

Nerdy Bunz
  • 6,040
  • 10
  • 41
  • 100
Vishesh Chandra
  • 6,951
  • 6
  • 35
  • 38

1 Answers1

2

The reason why you are having problems is not because of concurrency (because both methods are called sequentially in the UI thread), but because of the fact that your two animations are started at the same time. So what you need to do is to setAnimationListener to your animation and disallow click events till onAnimationEnd(Animation animation) is not invoked. This way you would have only one animation playing in current moment.

Desert
  • 2,293
  • 15
  • 16
  • This is the my question, how to prevent onClick of all buttons while any of button's onClick execution is in process?. – Vishesh Chandra Jul 26 '13 at 11:43
  • Animation is applied only on button's visibility with alpha, but i am having problem with ScrollView, I tried second point which i mention in question, why is that not working? pls – Vishesh Chandra Jul 26 '13 at 11:48
  • then you should post code to your `addAndMoveToSecondPage(mProshopPageView, true);` method – Desert Jul 26 '13 at 11:49
  • Okay, so the main thing you are doing is `smoothScrollTo`, which in fact *IS* animation (you can verify this in HorizontalScrollView source code). Frankly, I don't see some method to subscribe for scrolling animation, so I think your best try would be too just dissallow onClick for the duration of the animation (which is 250ms, but unfortunately this constant is private, so it can change in other versions). – Desert Jul 26 '13 at 12:12