is it possible to override my default phone "back" button? I want to end application on my phone back button click. How can I do it? Thank you!
4 Answers
Every activity has a method that is basically your back button on your android. And of course, this method is "overridable".
Put this in your activity:
@Override
public void onBackPressed() {
//do whatever
}
To finish the activity, write this in the above overridden method:
finish();
To finish the application, write this in the above overridden method:
android.os.Process.killProcess(android.os.Process.myPid());
EDIT: I am assuming you want to also have it not to show up in your recent apps. In conjunction with using the "android.os.process..." I mentioned earlier, put this in your android manifest in the activity tag of your root activity.
android:excludeFromRecents="true"
So it will look like this:
<activity
name:=blah
...
android:excludeFromRecents="true"
>
...
...
</activity>
I used this and the android.os.process.killprocess and it worked. The app stopped and did not show up in my recent apps (which is, again, assuming what you wanted).
I got this answer from Android - Remove application from Recent Apps
-
OnBackPressed works pretty good but android.os.Process.killProcess(android.os.Process.myPid()); does not work it behaves like finish(); – user3297991 Apr 08 '14 at 20:48
-
Do you meant that you don't want your app to show in your "recent apps"? I have edited this post. I think others have a better answer than me though. – Cherry Apr 08 '14 at 21:25
Yes, you can use FLAG_ACTIVITY_CLEAR_TOP
flag when you launch your intent to clear every previous activities. So when the user press "Back" it will close the application.
Intent intent = new Intent(this, NewTop.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
For example, consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B.
The currently running instance of activity B in the above example will either receive the new intent you are starting here in its onNewIntent() method, or be itself finished and restarted with the new intent. If it has declared its launch mode to be "multiple" (the default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same intent, then it will be finished and re-created; for all other launch modes or if FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be delivered to the current instance's onNewIntent().
This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. This is especially useful, for example, when launching an activity from the notification manager.
Also if you don't know every time an user press the back button, onBackPressed
is called so override it if you want to do something of different (but still, you should use the way i said above to do what you want).

- 14,682
- 8
- 43
- 53
Helpful Android - How To Override the "Back" button so it doesn't Finish() my Activity?
You are looking at using the android onKeyDown method and catching the back button press. http://developer.android.com/reference/android/app/Activity.html#onKeyDown
once you have done that you are wanting to take the application context and finish().
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
getApplicationContext().finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
Notes:
- This class must extend activity which is nessesary to access the back key press methods.
- You may have something like Context context = getActivityContext() somewhere in your code, this is not the correct context as ending this context would just close the activity at the top of the stack
- Killing processes should not be done in this context, it is for forcing closed an application. Make sure you finish, this means everything necessary is saved and the application can be ended naturally instead of forced.

- 1
- 1

- 167
- 2
- 13
Override the
@Override
onBackPressed()
{
}
in your activity and do your stuff in this method it will be called when user presses back button

- 47
- 8