java - Is Possible for on Back Pressed method with alert dialog in fragment -


@override     public void onbackpressed() {               log.d("back button", "back button pressed");             alertdialog.builder ad1=new alertdialog.builder(getactivity());             ad1.setmessage("are sure want exit? ");             ad1.setcancelable(false);              ad1.setpositivebutton("yes", new dialoginterface.onclicklistener() {                 @override                 public void onclick(dialoginterface arg0, int arg1) {                     intent = new intent(getactivity(), loginactivity.class);                     startactivity(i);                  }             });               ad1.setnegativebutton("no", new dialoginterface.onclicklistener() {                 @override                 public void onclick(dialoginterface arg0, int arg1) {                      intent = new intent(getactivity(), frndsearchfragment.class);                     startactivity(i);                  }             });               alertdialog alert=ad1.create();             alert.show();          } 

by using code getting error in @override , write super.onbackpressed got onbackpressed in super.onbackpressed .

yes can same overriding onkeydown method.

@override public boolean onkeydown(int keycode, keyevent event) {     if(keycode == keyevent.keycode_back) {         alertdialog.builder builder = new alertdialog.builder(this);         builder.setcancelable(false);         builder.settitle("exit");         builder.setmessage("do want exit application?");         builder.setpositivebutton("yes", new dialoginterface.onclicklistener() {             @override             public void onclick(dialoginterface dialog, int which) {                 dialog.dismiss();                 exit();             }         });         builder.setnegativebutton("no", new dialoginterface.onclicklistener() {             @override             public void onclick(dialoginterface dialog, int which) {                 dialog.dismiss();             }         });         alertdialog dialog = builder.create();         dialog.show();         return true;     }     return super.onkeydown(keycode, event); }  private void exit() {     finish(); } 

Comments