How to disable back button pressed in android fragment class

后端 未结 9 1166
别跟我提以往
别跟我提以往 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:10

    In your oncreateView() method you need to write this code and in KEYCODE_BACk return should true then it will stop the back button option for particular fragment

         View v = inflater.inflate(R.layout.xyz, container, false);
        //Back pressed Logic for fragment  
        v.setFocusableInTouchMode(true);  
        v.requestFocus();  
        v.setOnKeyListener(new View.OnKeyListener() {  
        @Override  
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
    
                    return true;  
                }  
            }  
            return false;  
        }  
    });
    

提交回复
热议问题