Just use the following code with initializing a field
private int count = 0;
@Override
public void onBackPressed() {
count++;
if (count >=1) {
/* If count is greater than 1 ,you can either move to the next
activity or just quit. */
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
finish();
overridePendingTransition
(R.anim.push_left_in, R.anim.push_left_out);
/* Quitting */
finishAffinity();
} else {
Toast.makeText(this, "Press back again to Leave!", Toast.LENGTH_SHORT).show();
// resetting the counter in 2s
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
count = 0;
}
}, 2000);
}
super.onBackPressed();
}