Domanda

ciao tutto quello che voglio intercettare il mio pulsante Home.
Quello che voglio è che ogni volta che premo un pulsante Home, voglio visualizzare una finestra di dialogo di avviso per essere sicuro di voler uscire.Se sì, quindi finisci l'attività non far nulla. Devo saperlo Quando mai premecciamo il tasto Home, vengono eseguite le seguenti callback in ordine.

OnSaveInstancestate (Bundle Outstate)

onpausa ()

Onstop ()

Quindi ho sovrascritto il metodo OnSaveInstancestate e impostare il mio codice di dialogo Alert lì ma mi dà un'eccezione sulla finestra di dialogo.Per favore aiuta gli amici.Guidami un lil..about.

Aggiornato:

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

DisplayLertDialog 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;

    }
.

È stato utile?

Soluzione

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..

Altri suggerimenti

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.

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);
     } 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top