反应本机链接到网页并按回按钮 [英] React Native Linking to web page and push back button

查看:36
本文介绍了反应本机链接到网页并按回按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个 react-native 应用程序,如果用户点击一个链接,那么应用程序会打开一个默认的网络浏览器并转到该链接.

I built a react-native app and if user clicks a link then app opens a default web browser and go to the link.

Linking.openURL(notification.link);

如果用户在 android 或 ios 设备上按下后退按钮,我们有什么办法可以检测到后退动作吗?

If user push back button from the android or ios devices, is there any way we can detect the back move?

推荐答案

你可以在 React Native 中添加一个监听器.

共有 4 个监听器.

There are total 4 listners for the same.

  1. willFocus - 屏幕将聚焦
  2. didFocus - 屏幕聚焦(如果有过渡,则过渡已完成)
  3. willBlur - 屏幕将无法聚焦
  4. didBlur - 屏幕未聚焦(如果有过渡,则过渡已完成)
  1. willFocus - the screen will focus
  2. didFocus - the screen focused (if there was a transition, the transition completed)
  3. willBlur - the screen will be unfocused
  4. didBlur - the screen unfocused (if there was a transition, the transition completed)


试试下面的代码


Try the below code

componentWillMount(){
    const didFocusSubscription = this.props.navigation.addListener(
      'didFocus',
      payload => {
        console.warn('didFocus ', payload);
        # just write your code/call a method here which you want to execute when the app comes from background
      }
    );
}

完成后不要忘记将其删除.

Dont forget to remove it when it is done.

componentWillUnmount(){
    didFocusSubscription.remove();
}

您可以阅读更多这里.谢谢:)

More you can read here. Thanks :)

这篇关于反应本机链接到网页并按回按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆