简体   繁体   中英

Back button double click

I am new android developer. I want to ask a question. Here is what I need: When the user click Back Button it counts as double click?

@Override public void onBackPressed() { --> what to write here? return; } }

You need to check an interval between to presses and determine whether you it can be counted as a double click or not:

private static final long DOUBLE_PRESS_INTERVAL = /* some value in ns. */;
private long lastPressTime;

@Override
public void onBackPressed() {
    long pressTime = System.nanoTime();
    if(pressTime - lastPressTime <= DOUBLE_PRESS_INTERVAL) {
        // this is a double click event
    }
    lastPressTime = pressTime;
}

You should probably include the reasoning behind wanting this functionality in the question instead of a comment. It makes it a lot easier for us to point you in the right direction. There are a few ways to achieve what you want but I would not recommend the 'double back' method.

Instead, if you show the progress bar in a dialog or somewhere in the search activity, there is no activity between the search and the second activity. That way you do not need to do a double back.

Also, you could display the progress bar in the second activity until the work is done and then replace it with the actual content with another call to setContentView(View) . Note that this would require threading though (otherwise the progress bar would never show).

Double Key Button

private static long back_pressed;

@Override
public void onBackPressed()
{
        if (back_pressed + 2000 > System.currentTimeMillis()) super.onBackPressed();
        else Toast.makeText(getBaseContext(), "Press once again to exit!", Toast.LENGTH_SHORT).show();
        back_pressed = System.currentTimeMillis();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM