30

我想禁用片段类中的后退按钮。onBackPressed()在这个片段中似乎不起作用。如何禁用后退按钮?

这是我的示例代码:

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() {
    }
}
4

11 回答 11

22

您必须覆盖父 FragmentActivity 类的 onBackPressed。因此,将您的代码放在父 FragmentActivity 中。或者您可以使用以下方法调用父母的方法:

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

在 FragmentActivity 重写 onBackPressed 方法并且不调用其超类来禁用后退按钮。

@Override
public void onBackPressed() {
  //super.onBackPressed();
  //create a dialog to ask yes no question whether or not the user wants to exit
  ...
}
于 2013-07-19T06:13:15.993 回答
14

onBackPressed()这是您可以通过新的活动回调来管理您的片段的新方法:

    // Disable onBack click
    requireActivity().onBackPressedDispatcher.addCallback(this) {
      // With blank your fragment BackPressed will be disabled.
    }

这是 android 文档链接:https ://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher

于 2020-06-16T12:26:39.350 回答
12

在您的 oncreateView() 方法中,您需要编写此代码,并且在 KEYCODE_BACk 返回应该为 true 然后它将停止特定片段的后退按钮选项

     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;  
    }  
});
于 2016-03-21T10:40:01.510 回答
11

在您的父活动中

@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();
    }
}
于 2015-06-29T11:36:51.873 回答
10

OnBackPressedCallback

这是您可以在 Fragment 类中编写以自定义后退按钮按下的代码。

public class MyFragment extends Fragment{

    @Override
    public void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);

        OnBackPressedCallback callback = new OnBackPressedCallback(true /* enabled by default */) {
            @Override
            public void handleOnBackPressed() {
                // Handle the back button even
                Log.d("BACKBUTTON", "Back button clicks");
            }
        };

        requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);

     }

}

相关链接

您可以在此处阅读和研究更多信息

于 2020-06-09T19:21:34.333 回答
8

我知道为时已晚,在片段 onCreate

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

callback.isEnabled
于 2019-07-14T10:34:20.577 回答
4

改变

public void onBackPressed() {
}

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

或者

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {

    }
    return super.onKeyDown(keyCode, event);    
}
于 2013-07-19T05:57:42.297 回答
4
@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;
}
于 2014-03-28T08:23:39.440 回答
1

如果您想在没有任何按钮操作的情况下永远不会关闭片段,您可以尝试防止取消片段。

在你的片段方法中添加这个

 this.setCancelable(false);
于 2020-05-09T01:42:27.273 回答
0

onBackPressed只需在要禁用后按的片段中覆盖即可。

更多信息在这里:https ://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher

public class Login extends Fragment {
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        OnBackPressedCallback callback = new OnBackPressedCallback(true) {
            @Override
            public void handleOnBackPressed() {
                // Log.d("TAG", "Pressed...");
            }
        };
        requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);
    }
}
于 2022-01-17T17:37:21.163 回答
-1

只需放置一个透明视图并为其设置 onClickListener

于 2021-01-27T11:27:18.603 回答