Introduction
Back key is frequently used while navigating to different screens (activities for a developer). It is important for any app developer to have some understanding of what events result out of back key press, how will these be handled by the Android OS and by their app, do they need to add specific handler methods to their code. etc.?
Background
Often beginner developers face the problems of apps crashing or other unexpected behaviour when back key is pressed from one of the activities of their app. By understanding some basic concepts, they will easily be able to get rid of these problems, and take total control over their app's behaviour (which should always be the case). The article is by no means comprehensive on this subject but helps to understand some key points.
The Code
When back key is pressed on an activity in focus, the OS or the system itself issues the finish()
call for that activity, allowing the app to return to the calling activity, and ensuring the parent activity is restored in the same state in which it was before the child activity was called. So, essentially a developer does not need to do anything to handle back key presses.
But sometimes the app crashes, or the parent activity is not in the same state as it was before. The developer (being a developer) is bound to feel that he/she needs to handle the back press event to address the issue. A quick seach on browser leads him/her to developers' forums, and there are often solutions suggested to add a handling method. Two common solutions you may come across are:
-
@override
public void onBackPressed(){
super.onBackPressed();
finish();
}
-
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
return false;
}
return super.onKeyDown(keyCode, event);
}
Most likely you don't need any of these. The activity will be finished by itself, and parent activity will be restored. If the child activity was called with StartActivity()
, you may observe no problems. It's when your activity was supposed to return some results via an intent, you may run into issues. The real issue may be in the calling activity rather than the called one. A simple understanding of what happens when back key is pressed on an activity which was called with startActivityForResult(Intent, int, Bundle)
will be enough for you to know what you need to fix in your calling activity. Read on.
What you may not be expecting (at least I did not), that when back key is pressed on an activity which is supposed to return some results , the activity is finished at that point , and the control is returned to the Parent activity inside the onActivityResult()
handler. So irrespective of whether the child activity finished as it was designed to, or it finished due to a back press, the control returns to onResult
method in calling activity. This means you need to put the right code in this method to be able to handle the back press event.
The most common reason for app crash in such scenarios is that the intent passed to the called activity is returned null
when back key is pressed, and the onActivityResult()
has some code which is trying to access the data in the intent. Unless the null
intent exception is handled, it has no option but to crash.
As long as you are checking the result code before using the data in the intent, you should have no problems, like below:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_CONTACT_REQUEST) {
if (resultCode == RESULT_OK) {
}
}
}
To summarize, the key points to know, when back key is pressed on called activity are:
finish()
is automatically issues on called activity. - Control returns to
onActivityResult
method of calling activity when back key is pressed on called activity.
You can handle rest past as long as you are aware of this.