简体   繁体   English

反应原生导航 v5 android 后退按钮处理程序

[英]React native navigation v5 android back button handler

I'm using react navigation v5 and I'm trying to make a default android back button handler inside routes.js.我正在使用 react navigation v5,我正在尝试在 routes.js 中创建一个默认的 android 后退按钮处理程序。 Using flux there was backAndroidHandler param which let you do this.使用flux 有backAndroidHandler 参数可以让你做到这一点。 My questios is there something i can use in navigation v5.我的问题是我可以在导航 v5 中使用的东西。

I know there is https://reactnavigation.org/docs/custom-android-back-button-handling/ .我知道有https://reactnavigation.org/docs/custom-android-back-button-handling/ But the problem is i don't want to go to every screen and set it.但问题是我不想去每个屏幕并设置它。

Current solution is this:目前的解决方案是这样的:

  useEffect(
    useCallback(() => {
      const onAndroidBack = () => {
      };

      BackHandler.addEventListener('hardwareBackPress', onAndroidBack);

      return () =>
        BackHandler.removeEventListener('hardwareBackPress', onAndroidBack);
    }, [])
  );

My question does navigation v5 have some kind of solution for this build in, like flux had?我的问题导航 v5 是否有针对此内置的某种解决方案,例如flux? Is there a better way to manage android back button?有没有更好的方法来管理android后退按钮?

The solution the docs have in your link seem to be the best bet but I see one step missing.文档在您的链接中提供的解决方案似乎是最好的选择,但我发现缺少一步。

You can make ScreenWithCustomBackBehavior return its children, making it a container instead of a component.您可以让ScreenWithCustomBackBehavior返回其子项,使其成为容器而不是组件。 Then wrapping this around each screen you need to customize does not seem to be overkill.然后将其环绕在您需要自定义的每个屏幕上似乎并不过分。 And that solution is really if you need it to happen on focus.这个解决方案真的是如果你需要它发生在焦点上。 If you need a global android back button listener you can register it at the top level of your app.如果您需要全局 android 后退按钮侦听器,您可以在应用程序的顶层注册它。

Whatever the generally agreed upon solution will be, it will most likely either take the form of a container (like ScreenWithCustomBackBehavior ) or registered listeners at the top level.无论普遍同意的解决方案是什么,它很可能采用容器的形式(如ScreenWithCustomBackBehavior )或在顶级注册侦听器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM