How to disable back button pressed in android fragment class

后端 未结 9 1167
别跟我提以往
别跟我提以往 2020-12-11 14:39

I want to disable the back button in a fragment class. onBackPressed() doesn\'t seem to work in this fragment. How could I disable the back button?

This

9条回答
  •  借酒劲吻你
    2020-12-11 15:08

    You have to override onBackPressed of parent FragmentActivity class. Therefore, put your codes in parent FragmentActivity. Or you can call parent's method by using this:

    public void callParentMethod(){
        getActivity().onBackPressed();
    }
    

    in FragmentActivity override onBackPressed Method and not call its super class to disable back button.

    @Override
    public void onBackPressed() {
      //super.onBackPressed();
      //create a dialog to ask yes no question whether or not the user wants to exit
      ...
    }
    

提交回复
热议问题