Question

I didn't know how to phrase the question properly so let me explain. I have noticed that on the android platform that when you press two buttons in one activity in quick sequence (press one button then the other before the activity has a chance to leave the screen) that two activities are called one after another. It is not visible while it happens but if you press the back button then the activity that was called with the second button leaves (finishes) and the activity that was called with the first button shows up. You have to go back again to go back to the calling Activity. So you have to press back twice to get to Activity 2's parent activity.

So I want to know if this is a problem for others and if so how would you go about fixing that. Or do you think this is not much of a problem.

Was it helpful?

Solution 2

I have figured out how to pass this problem. I have a base activity class that every other activity in my application inherits from. I override

onResume with:

@Override
protected void onResume()
{
    super.onResume();
    setCanStartNewActivity(true);
}

and startActivityForResult with:

@Override
public void startActivityForResult(Intent intent, int requestCode)
{
    //Does not allow starting a new activity unless previous activity returned
    //This is a trick to stop multiple simultaneous button presses starting multiple 
    //activities simultaneously.
    if(!canStartNewActivity)
        return;
    setCanStartNewActivity(false);
    super.startActivityForResult(intent, requestCode);
}

This basically makes sure only one new activity can be started for a button press. If you let go of two buttons at the same time only the button released the earliest (even by 10 microseconds) will be fired and the other will still send the intent but it will not fire until the first fired activity returns or the app dies.

OTHER TIPS

You can call finish() after startActivity(your_intent); that way the activity you are leaving finishes and you get to the next one, so you dont have to press back twice to get to it's parent Activity.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top