How to change the back button behaviour

ometimes, when developing Android applications, there is a need to override the default behaviour of the back button which means assigning another behavior for the ‘Back’ button. Although changing the behaviour of back button is not recommended, but there are some situations where changing the ‘back’ button behavior is mandatory, such as to avoid accidentally finishing or closing the current Activity.

For example, a text editor, should confirm if the user really wants to quit without saving the current changes, or a game, that check if it is the player’s intention to close the current game session.


Quick Links
Let us write few lines of code to change the default behavior of ‘back’ button. To start with, I assume you are going to test our code in Android devices having OS version 2.0 or above.

Code Listings

There is a ‘back’ key press event available in Android with which we can assign different behaviour for ‘back’ button press event:

1
2
3
4
5
@Override
public void onBackPressed() {
     //Include the code here
     return ;
}

For lower versions below Android 2.0, we need to handle the ‘back’ button press event in different way as there is no onBackPressed() method available.

All we need to do is, override the onKeyDown() method and check if ‘back’ button has been pressed:

1
2
3
4
5
6
7
8
9
@Override 
public boolean onKeyDown( int keyCode, KeyEvent event){ 
         //Changes 'back' button action 
         if (keyCode==KeyEvent.KEYCODE_BACK) 
        
          //Include the code here
        
         return true
     }

I just created a simple application which will ask the user to confirm leaving the application when the user presses the ‘back’ button:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.prgguru.example;
 
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
 
public class AndroidBackButtonActivity extends Activity {
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.main);
         Toast.makeText(getApplicationContext(), "http://android.programmerguru.com" , Toast.LENGTH_LONG).show();
     }
 
     @Override
     public void onBackPressed() {
         //Display alert message when back button has been pressed
         backButtonHandler();
         return ;
     }
 
     public void backButtonHandler() {
         AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                 AndroidBackButtonActivity. this );
         // Setting Dialog Title
         alertDialog.setTitle( "Leave application?" );
         // Setting Dialog Message
         alertDialog.setMessage( "Are you sure you want to leave the application?" );
         // Setting Icon to Dialog
         alertDialog.setIcon(R.drawable.dialog_icon);
         // Setting Positive "Yes" Button
         alertDialog.setPositiveButton( "YES" ,
                 new DialogInterface.OnClickListener() {
                     public void onClick(DialogInterface dialog, int which) {
                         finish();
                     }
                 });
         // Setting Negative "NO" Button
         alertDialog.setNegativeButton( "NO" ,
                 new DialogInterface.OnClickListener() {
                     public void onClick(DialogInterface dialog, int which) {
                         // Write your code here to invoke NO event
                         dialog.cancel();
                     }
                 });
         // Showing Alert Message
         alertDialog.show();
     }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值