When a user presses the back button on an intent, the application should quit. How can I ensure the application quits when the back button is pressed?
In order to check when the 'BACK' button is pressed, use onBackPressed() method from the Android library. Next, perform a check to see if the 'BACK' button is pressed again within 2 seconds and will close the app if it is so. Otherwise, don't exit.
Get back press work only at second press and notify user to press again to exit.
How do I close all activities on Android? exit(); or finish(); it exit full application or all activity.
In my Home Activity I override the "onBackPressed" to:
@Override public void onBackPressed() { Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); }
so if the user is in the home activity and press back, he goes to the home screen.
I took the code from Going to home screen Programmatically
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With