2

hi all i want to intercept my Home Button.
what i want is that whenever i press a Home Button i want to display a Alert dialog for Are you sure you want to Quit. if Yes then Finish the activity else do nothing. I have got to know that
when ever we Press Home Button the following callbacks are performed in order.

onSaveInstanceState(Bundle outState)

onPause()

onStop()

So i have overRide onSaveInstanceState Method and set my Alert dialog Code there but it gives me Exception on Dialog. Please Help friends. Guide me a lil..about it.

UPDATED:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
boolean flag = displayAlertDialog();
        if(flag){
             this.finish();
         super.onSaveInstanceState(savedInstanceState);
        }
}  

displayAlertDialog Method:

private boolean isExit = false;
public boolean displayAlertDialog()
    {
        //final boolean flag=true;
        int a = 0;
        AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
        alt_bld.setMessage("Are you sure you want to Exit?")
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                isExit = true;
            }

        })
        .setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                isExit = false;
                dialog.cancel();
            }
        });
        alt_bld.show();
    return isExit;

    }
Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78
Shah
  • 4,990
  • 10
  • 48
  • 70

3 Answers3

3

you cant open dialog on home button. but you can open any activity on home button by user choose action like home or your activity..

Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
  • no. when ever we press Home Key onKeyDown is not envoked. i have checked on it. – Shah May 19 '11 at 05:50
  • yes its working on Back Key but whenever i press Home. onKeyDown does not envoked. – Shah May 19 '11 at 05:54
  • bro its not even come in onKeyDown Method. onKeyDown Does not envoked. on Home Button:( thats the problem dude – Shah May 19 '11 at 06:01
  • ohhh so u cant open directly dialog on home, but optional like user can choose home or dialog.. – Niranj Patel May 19 '11 at 06:04
  • PLease Help Bro with ur knowledge.. i have to intercept Home BUtton – Shah May 19 '11 at 06:11
  • it is possible but it will be create issue. bcz whenever you press home key then always your dialog will display. in short in any app your press home then dialog will be open not only in your app – Niranj Patel May 19 '11 at 06:17
  • http://stackoverflow.com/questions/2079691/overriding-the-home-button-how-do-i-get-rid-of-the-choice read it – Niranj Patel May 19 '11 at 06:23
1

AFAIK KeyCode.KEYCODE_HOME is never propagated to Activty/Dialog when HOME key is pressed, HOME key is intercepted in framework to guarantee homescreen/activity registered for "android.intent.category.HOME" always brought to focus.

If intercepting HOME key is allowed in applications, there is a possibility that an evil app can prevent the user from exiting the application and using the phone functionality.

Suresh
  • 9,495
  • 14
  • 49
  • 63
0

Try this mate....

 @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
        if ((keyCode == KeyEvent.KEYCODE_HOME)) {    
         Log.d(this.getClass().getName(), "home button pressed");   
      }  
       return super.onKeyDown(keyCode, event);
     } 
Krishna
  • 1,454
  • 5
  • 16
  • 31