Android: How to override onBackPressed() in AlertDialog?

后端 未结 4 1459
[愿得一人]
[愿得一人] 2021-02-05 04:30

I have an AlertDialog dlgDetails which is shown from another AlertDialog dlgMenu. I would like to be able to show dlgMenu again if the user presses the

4条回答
  •  萌比男神i
    2021-02-05 05:09

    I created a new function within the java class and made a call to that function from the onClick method of the dialog Builder.

    public class Filename extends Activity(){
    
    @Override
    onCreate(){
     //your stuff
     //some button click launches Alertdialog
    }
    
    public void myCustomDialog(){
     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
     //other details for builder
          alertDialogBuilder.setNegativeButton("BACK",
                new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                             dialod.dismiss;
                             myCustomBack();
                        }
          });
    
     AlertDialog alertDialog = alertDialogBuilder.create();
     alertDialog.setCanceledOnTouchOutside(true);
     alertDialog.show();
    }
    
    public void myCustomBack(){
      if(condition1){
        super.onBackPressed();
      }
      if(condition 2){
        //do  stuff here
      }
    }
    
    @Override
    public void onBackPressed(){
      //handle case here
      if (condition A)
        //Do this
      else 
        //Do that
    }
    
    }
    

提交回复
热议问题