Android development step by step 22: process the back button event in the Activity
In the mobile app, the user clicks the back button to return to the previous page. Generally, the page does not need to be processed. If you click back on the home page and there is no prompt, the app is closed, this user experience is not very good, so the user is generally prompted to confirm: exit? Avoid misoperation.
I. Processing in Activity
@ Override
Public boolean onKeyDown (int keyCode, KeyEvent event ){
// TODO Auto-generated method stub
Toast. makeText (this, "onkeydown", Toast. LENGTH_SHORT). show ();
If (keyCode = KeyEvent. KEYCODE_BACK & event. getRepeatCount () = 0 ){
Toast. makeText (this, "onbackkeydown", Toast. LENGTH_SHORT). show ();
AlertDialog. Builder alertBuilder = new AlertDialog. Builder (this );
AlertBuilder. setTitle ("prompt message"). setMessage (R. string. menu_exit_desc)
. SetCancelable (false). setPositiveButton (R. string. OK,
New DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog,
Int id ){
AActivity. this. finish ();
Android. OS. Process. killProcess (android. OS. Process
. MyPid ());
}
}). SetNegativeButton (R. string. cancel,
New DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog,
Int id ){
Dialog. cancel ();
}
});
Return false;
}
Return super. onKeyDown (keyCode, event );
}
/* This rollback button event starts at 2.1 and can be processed here.
Called when the activity has detected the user's press of the back key.
The default implementation simply finishes the current activity,
But you can override this to do whatever you want.
*/
@ Override
Public void onBackPressed (){
// TODO Auto-generated method stub
AlertDialog. Builder alertBuilder = new AlertDialog. Builder (this );
AlertBuilder. setTitle ("prompt message"). setMessage ("are you sure you want to exit? ")
. SetCancelable (false). setPositiveButton ("OK ",
New DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog,
Int id ){
MainActivity. this. finish ();
Android. OS. Process
. KillProcess (android. OS. Process
. MyPid ());
}
}). SetNegativeButton ("cancel ",
New DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog,
Int id ){
Dialog. cancel ();
}
});
AlertBuilder. show ();
}
2. Processing in TabActivity
It is found that the OnKeyDown time in TabActivity is not monitored, mainly because the focus of the current activity is in the sub-activity in the tab and the desired focus is not obtained in TabActivity, so the key-pressing operation does not work.
After reading the SDK documentation, I found that there is a dispatchKeyEvent (KeyEvent event). You can listen to the event operation and then determine which button is used to perform corresponding key operations, note that an event must be added to the if statement. getAction () = KeyEvent. ACTION_DOWN judgment.
@ Override
Public boolean dispatchKeyEvent (KeyEvent event ){
// TODO Auto-generated method stub
Toast. makeText (this, "test1", Toast. LENGTH_SHORT). show ();
If (event. getAction () = KeyEvent. ACTION_DOWN & event. getKeyCode () = KeyEvent. KEYCODE_BACK)
{
AlertDialog. Builder alertBuilder = new AlertDialog. Builder (this );
AlertBuilder. setTitle ("prompt message"). setMessage (R. string. menu_exit_desc)
. SetCancelable (false). setPositiveButton (R. string. OK,
New DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog,
Int id ){
HomeActivity. this. finish ();
Android. OS. Process
. KillProcess (android. OS. Process
. MyPid ());
}
}). SetNegativeButton (R. string. cancel,
New DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog,
Int id ){
Dialog. cancel ();
}
});
AlertBuilder. show ();
Return false;
}
Return super. dispatchKeyEvent (event );
}
Iii. Effect