How to pick up the events of the Back key on the Android app
In the Android app, (receive) event method of picking up the key has been pressed the Back button (Back) What is it?
Before delving into the subject, it will only talk a little averted.
Is to update the site Senki "Android of mucchin" for quite a long time.
It is about 50 days pretending.
For busy practice that has become, was also stops working an Android app development was done in private.
Only a little spare time so finally, I tried to to continued development of Android apps had been made from previous over.
And is freed a little while expected, there were so undeniable that some blank.
Can not explain what was happening on Android, has been forgotten and that the making of fine had built his Android app, which is as much. (Laughs)
So, I would like to get down to business.
This time, in the Android app, is about how to pick up the event when it is pressed the Back key.
I have now has been making an Android app, with features GPS, I tried to install and test it to XPERIA.
For example, even if the GPS positioning indoors, does not end positioning can not be processed.
I noticed that in that state, even if that ends the Activity icon in the icon, such as positioning it at the top of the screen of the XPERIA that has been turned to leave Pikopiko and movement.
I thought so I thought there, and forget the positioning, and indeed whether intact, and I want to cancel the GPS positioning at the end of Activity.
Android app was installed on your own "Back" button, received the event has been pressed, of course, can be processed like. (. In onClickListener)
Here, by putting the cancellation process of positioning GPS, Activity at the end of your own by hitting back button is no problem, gave me to cancel the positioning.
But, this alone, if the key has been pressed the Back of the hard side, the positioning operation is not canceled.
Let's talk when pressed the Back key that is present as a hard key, and what I do.
OnDestroy of Activity () event
What do you want to do a Back key has been pressed? I think that depending on the purpose, and how to handle different.
Like me, at the end of Activity, in the case of processing, you can implement () method, you may want to describe the process onDestroy of Activity or want to cancel the operation you are running, Tokayuu you want to release something.
Activity is at the end, onDestroy event occurs.
Of course, even after the quit () method, because it occurs onDestroy this event finish, in the process that you put in a button press exit to own, if you have put a process such as canceled Yara release, onDestroy () I think a good idea to implement.
OnKeyDown of Activity () event
This section describes, what to do when "I want to ignore the Back key."
However, because I think this process, and unkind to the user, so is not recommended.
When the key is pressed, so onKeyDown event occurs, the Activity, to implement () method onKeyDown.
Then, for example, the following code.
public boolean onKeyDown (int keyCode, KeyEvent event) {
if (keyCode! = KeyEvent.KEYCODE_BACK) {
return super.onKeyDown (keyCode, event);
} Else {
return false;
}
}
KeyEvent.KEYCODE_BACK is a constant of android.view.KeyEvent class, shows the Back key.
Back when the key was, by, and not to call () onKeyDown of the super class, the processing of Back key, this means you are finished with this section, you will not be returning to the previous screen.
If, if you implement such processes, etc. to display a message like "., Please exit by pressing a button instead of the Back key to return", but please raise that you do not bother the user earnestly .
In the Android emulator, because it was not like icon icon is displayed in the positioning of the GPS, I was thought no problem, looking at moving in the XPERIA fact, you will find the problem and trickles.
If you're developing an Android app is also everyone, it is recommended to obtain the actual means.
And also different because the screen size, and move it in the emulator, it is because a run on the XPERIA, it is the difference between Yappari atmosphere.











