How to disable back button pressed in android fragment class

后端 未结 9 1142
别跟我提以往
别跟我提以往 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条回答
  • Change

    public void onBackPressed() {
    }
    

    to

    @Override
    public void onBackPressed() {
        super.onBackPressed()
    }
    

    OR

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
    
        }
        return super.onKeyDown(keyCode, event);    
    }
    
    0 讨论(0)
  • 2020-12-11 15:22
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if ( keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
    
        onBackPressed();
    }
    
    return super.onKeyDown(keyCode, event);
    }
    @Override
    public void onBackPressed() {
    
    return;
    }
    
    0 讨论(0)
  • 2020-12-11 15:25

    In your parent Activity

    @Override
    public void onBackPressed() {
    
        Fragment f = getSupportFragmentManager().findFragmentById(R.id.content_frame);
        if (f instanceof yourfragment) {//the fragment on which you want to handle your back press
            Log.i("BACK PRESSED", "BACK PRESSED");
        }else{
            super.onBackPressed();
        }
    }
    
    0 讨论(0)
提交回复
热议问题