Android: Fast button presses results in multiple i

2020-07-18 07:55发布

问题:

Currently I'm experiencing a bug where if the user quickly taps a button, the intent that the button is attached to will fire off multiple times, resulting in a stack of that intent that will need to be back traced through again. How can I avoid this or remedy this?

Thanks ~k

This is inside of the onClickListener. I set the boolean value here, then I unset it at the end of the process.

        if(!isDating)
        {
            intent.setClass(context, EventDate.class);
            isDating = true;
            ((TabGroupActivity)
            context).startChildActivity("EventDate",intent);
        }

回答1:

Try setting the flags for the intent like

intent.setFlags(FLAG_ACTIVITY_BROUGHT_TO_FRONT);

You can also set this flag via AndroidManifest.xml file in the Application section. Prefer this method over above one.

Updating launchMode using the Manifest file

Hope this resolves your issue.



回答2:

If i had a dime for this bug my QA filed I would be pretty rich if not a millionaire :P There are only so much one can do. Based on your implementation you could try a few things.

  1. As already mentioned, use a boolean. Set it to true once you click it and check this boolean if the button is clicked again. Set it to false once you are done.

  2. Use a progress dialog in case its a long activity like receiving data in your next activity before it displays. This also gives a hint to the user, that something is happening and he need not worry.



回答3:

use

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

it fixes the problem



回答4:

Actually I found a better solution!

by setting the onClickListener(null); then recreating it onResume, it bypasses having to use flags and what not.

!k