How to disable back button pressed in android frag

2020-04-02 03:10发布

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 is my sample code:

public class Login extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
       ,Bundle savedInstanceState) {
        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.login, null);
        return root;
    }

    public void onBackPressed() {
    }
}

6条回答
何必那么认真
2楼-- · 2020-04-02 03:19

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;  
    }  
});
查看更多
家丑人穷心不美
3楼-- · 2020-04-02 03:19

I know it's too late, In fragment onCreate

```

    val callback = requireActivity().onBackPressedDispatcher.addCallback(this) {
    Log.d("tag","back button pressed")    // Handle the back button event
    }

    callback.isEnabled
```
查看更多
▲ chillily
4楼-- · 2020-04-02 03:20

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);    
}
查看更多
We Are One
5楼-- · 2020-04-02 03: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();
    }
}
查看更多
唯我独甜
6楼-- · 2020-04-02 03:34

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
  ...
}
查看更多
手持菜刀,她持情操
7楼-- · 2020-04-02 03:38
@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;
}
查看更多
登录 后发表回答