Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid an item from Action Bar from being double clicked

I have designed an action bar for my Android app. In this action bar there's a button that launches a Dialog Activity used to configure my app's behavior. If I double click this button fast enough, I'm able to order the Dialog Activity to be launched twice before it actually appears, and then it appears duplicated and visually overlapped and I don't want this. I tried to create some sort of lock-down mechanism but it is not working because my Dialog Activity is launched only after all the code in my Main Activity calling method (onOptionsItemSelected) is executed. Is there a way to avoid this form happening?

My code is:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

//ensure only one element from the option menu is launched at once (if you double click fast you could launch two)

Log.e("test", "onOptionsItemSelected ");
if(optionItemAlreadySelected == false)
{
    optionItemAlreadySelected = true;

    int id = item.getItemId();

    if (id ==  R.id.action_sound_mode) {
        //item.setVisible(false);
        Intent intent = new Intent(this, SoundConfigurationActivity.class);

        startActivity(intent);

        optionItemAlreadySelected = false; //this code is executed before the activity is started!!!
        return true;
    }

}

return super.onOptionsItemSelected(item);
}

Is there a way to know when the Dialog Activity has already being closed and lock the opportunity to open it once again until then.

like image 736
VMMF Avatar asked Dec 02 '25 06:12

VMMF


2 Answers

Kotlin

It's a screen(Activity, Fragment) based solution to avoid a double tap on menu action.

  • Add below global variable to your activity/fragment containing onOptionsItemSelected function.

    private var previousClickTimeMillis = 0L
    
  • Write below function anywhere in the project i.e Utils.

    fun singleSafeClick(
    previousClickTimeMillis: Long,
    block: (previousClickTimeMillis: Long) -> Unit) {
    val currentTimeMillis = System.currentTimeMillis()
    if (currentTimeMillis < previousClickTimeMillis || currentTimeMillis >= previousClickTimeMillis + OnSingleClickListener.DELAY_MILLIS) {
     block(currentTimeMillis)
     }
    }
    
  • Write your triggering code as below.

     override fun onOptionsItemSelected(item: MenuItem): Boolean {
    
     when (item.itemId) {
         R.id.action_delete -> {
             singleSafeClick(previousClickTimeMillis) { tappedTime ->
                 previousClickTimeMillis = tappedTime
                // Write Yyur code here
             }
         }
     }
    }
    
like image 172
Muhammad Maqsood Avatar answered Dec 03 '25 21:12

Muhammad Maqsood


You can use a boolean variable to track the state of your Dialog. When you click the button you set mDialogShown = true to block any other show dialog requests.
Now when the user presses Back button and the Dialog is closed onActivityResult is called.
At this point your are sure that the Dialog was closed.
I assumed your code is inside an Activity:

class MainActivity extend Activity {

    static final int SHOW_DIALOG_REQUEST = 1;  // The request code
    static boolean mDialogShown = false;  // True if dialog is currently shown

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_sound_mode) {
            showDialog();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void showDialog() {
        if (!mDialogShown) {
            mDialogShown = true;
            Intent intent = new Intent(this, SoundConfigurationActivity.class);
            startActivityForResult(intent, SHOW_DIALOG_REQUEST);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Check which request we're responding to
        if (requestCode == SHOW_DIALOG_REQUEST) {
            mDialogShown = false;
        }
    }
}

Documentation
https://developer.android.com/training/basics/intents/result.html https://developer.android.com/guide/topics/ui/dialogs.html#ActivityAsDialog

like image 24
vovahost Avatar answered Dec 03 '25 19:12

vovahost



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!