6

I am creating an application here i need to finish activity when user twice pressed back button. Here is my code what i tried

 @Override
public void onBackPressed() {
        super.onBackPressed();
        this.finish();
}

tried this too

 @Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
    finish();
}
return super.onKeyDown(keyCode, event);
}

this helps me to finish activity once back button pressed.

Pls i need your suggestion. Thanks in advance

Aristo Michael
  • 2,166
  • 3
  • 35
  • 43

7 Answers7

16

Ok...here is a longer, but effective way of doing this...

1) Make a global vairable in your class like...

private boolean backPressedToExitOnce = false;
private Toast toast = null;

2) Then implement onBackPressed of activity like this...

@Override
public void onBackPressed() {
    if (backPressedToExitOnce) {
        super.onBackPressed();
    } else {
        this.backPressedToExitOnce = true;
        showToast("Press again to exit");
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                backPressedToExitOnce = false;
            }
        }, 2000);
    }
}

3) Use this trick to handle this toast efficiantly...

/**
 * Created to make sure that you toast doesn't show miltiple times, if user pressed back
 * button more than once. 
 * @param message Message to show on toast.
 */
private void showToast(String message) {
    if (this.toast == null) {
        // Create toast if found null, it would he the case of first call only
        this.toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);

    } else if (this.toast.getView() == null) {
        // Toast not showing, so create new one
        this.toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);

    } else {
        // Updating toast message is showing
        this.toast.setText(message);
    }

    // Showing toast finally
    this.toast.show();
}

4) And use this trick to hide toast when you activity is closed...

/**
 * Kill the toast if showing. Supposed to call from onPause() of activity.
 * So that toast also get removed as activity goes to background, to improve
 * better app experiance for user
 */
private void killToast() {
    if (this.toast != null) {
        this.toast.cancel();
    }
}

5) Implement you onPause() like that, to kill toast immidiatly as activity goes to background

@Override
protected void onPause() {
    killToast();
    super.onPause();
}

Hope this will help...:)

umair.ali
  • 2,714
  • 2
  • 20
  • 30
1

Use boolean to check if the button is pressed twice

boolean isFinsihActivity = false;

@Override
public void onBackPressed() {
    if (isFinsihActivity) {
        super.onBackPressed();
    }
    isFinsihActivity = true;
}
biniam
  • 8,099
  • 9
  • 49
  • 58
henry4343
  • 3,871
  • 5
  • 22
  • 30
1

I think this is the correct way is to wait for second back

private boolean _doubleBackToExitPressedOnce    = false;

@Override
public void onBackPressed() {

Log.i(TAG, "onBackPressed--");
if (_doubleBackToExitPressedOnce) {
    super.onBackPressed();
    return;
}
this._doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Press again to quit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {

        _doubleBackToExitPressedOnce = false;
    }
}, 2000);
}

This works for me... answer taken from here

Community
  • 1
  • 1
Aristo Michael
  • 2,166
  • 3
  • 35
  • 43
0

Maintain a global variable in your Activity

private int count;

Override onBackPressed in your activity.

@Override
public void onBackPressed() {
    if (count == 1) {
        super.onBackPressed();
    }
    count++;
}
Jagadesh Seeram
  • 2,630
  • 1
  • 16
  • 29
0

what I would do is use a timestamp for the first back click and if it happens again within the time limit I would finish.

long mtimeOfFirstClick
long TIME_LIMIT = 0.5 DateUtils.SECONDS_IN_MILLIS;

public void onBackPressed() {
    if(System.currentTimeMillis() - mtimeOfFirstClick <= TIME_LIMIT) {
        finish();
    } else {
        mtimeOfFirstClick = System.currentTimeMillis();
    }
}
KingAlex1985
  • 659
  • 1
  • 8
  • 9
thepoosh
  • 12,497
  • 15
  • 73
  • 132
0

try like this initialize the static int variable with 0 and increment the variable inside the onBackPressed() and check it value be 2;

public class MainActivity extends Activity {
static int i=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void onBackPressed(){
    i++;
    if(i==2){

        finish();

    }

}
Nambi
  • 11,944
  • 3
  • 37
  • 49
0

do it something like this:

int counter=0;

    @Override
    public void onBackPressed() {
            super.onBackPressed();
            counter+=1;
            if(counter==2){
            this.finish();}
    }
Hamad
  • 5,096
  • 13
  • 37
  • 65