Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exit when back button is pressed?

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?

like image 469
d-man Avatar asked Mar 01 '10 07:03

d-man


People also ask

How do I close an app when my 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.

How do I close a double pressed app in Android?

Get back press work only at second press and notify user to press again to exit.

How do you exit an activity?

How do I close all activities on Android? exit(); or finish(); it exit full application or all activity.


1 Answers

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

like image 146
Hugo Matilla Avatar answered Sep 23 '22 10:09

Hugo Matilla