2012-12-26 13 views
6

Mam działanie, w którym po naciśnięciu przycisku wstecz, nie jest wyświetlane okno dialogowe alertu. Jaki może być problem? Oto mój kodAktywność onBackPress nie wyświetla się okno dialogowe z ostrzeżeniem

@Override 
public void onBackPressed() { 
    // TODO Auto-generated method stub 
    super.onBackPressed(); 

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(LogFish.this); 

       // set title 
       alertDialogBuilder.setTitle("Exit"); 
       alertDialogBuilder.setIcon(R.drawable.ic_action_search); 

       // set dialog message 
       alertDialogBuilder 
        .setMessage("This action will cause you to abandon all changes on current new fish log. \n\nAre you sure you want to exit?") 
        .setCancelable(false) 
        .setPositiveButton("YES",new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog,int id) { 
          // if this button is clicked, close 
          startActivity(new Intent(LogFish.this,MainActivity.class)); 
          finish(); 

         } 
         }) 
        .setNegativeButton("NO",new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog,int id) { 
          // if this button is clicked, just close 

          dialog.cancel(); 
         } 
        }); 

        // create alert dialog 
        AlertDialog alertDialog = alertDialogBuilder.create(); 

        // show it 
        alertDialog.show(); 

        } 
+0

Proszę sprawdzić, czy klucz do tyłu nie jest ujęte w onKeyDown() .... –

+0

Sprawdź czy nie upaść, bo uruchomić w głównym wątku? – IamStalker

+2

Właściwie jest to wywoływane, ale natychmiast Twoja aktywność zostaje zakończona, ponieważ napisałeś 'super.onBackPressed();'. Jeśli usuniesz to, to twoja aktywność nie zakończy się z powrotem Naciśnij i pojawi się twoje okno dialogowe. – user370305

Odpowiedz

13

trzeba usunąć super.onBackPressed();

0
@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    //Handle the back button 
    if(keyCode == KeyEvent.KEYCODE_BACK) { 
     //Ask the user if they want to quit 
     new AlertDialog.Builder(this) 
     .setIcon(android.R.drawable.ic_dialog_alert) 
     .setTitle(R.string.quit) 
     .setMessage(R.string.really_quit) 
     .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 

       //Stop the activity 
       YourClass.this.finish();  
      } 

     }) 
     .setNegativeButton(R.string.no, null) 
     .show(); 

     return true; 
    } 
    else { 
     return super.onKeyDown(keyCode, event); 
    } 

} 
2
@Override 
public void onBackPressed() { 

    final AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setTitle("Title"); 
    builder.setMessage("Your Message"); 
    builder.setIcon(android.R.drawable.ic_dialog_alert); 

    builder.setPositiveButton("YES", new OnClickListener() { 

     public void onClick(DialogInterface dialog, int which) { 
      //implement your logic for YES 
     } 
    }); 

    builder.setNegativeButton("NO", new OnClickListener() { 

     public void onClick(DialogInterface dialog, int which) { 
      //implement your logic for NO 
     } 
    }); 
    builder.setOnCancelListener(null); 
    builder.show(); 
} 
Powiązane problemy