///====================================================
private static final int TIME_INTERVAL = 2000; // # milliseconds, desired
private long mBackPressed;
// When user click bakpress button this method is called
@Override
public void onBackPressed() {
// When user press back button
if (mBackPressed + TIME_INTERVAL > System.currentTimeMillis()) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
} else {
Toast.makeText(getBaseContext(), "Press again to exit",
Toast.LENGTH_SHORT).show();
}
mBackPressed = System.currentTimeMillis();
} // end of onBackpressed method
Another One
@Override
public void onBackPressed() {
//super.onBackPressed();
new AlertDialog.Builder(MainActivity.this)
.setTitle("Do you want to exit!")
.setNegativeButton("No Yaar!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setPositiveButton("Yea Bro!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
})
.show();
}