gpt4 book ai didi

reactjs - 返回 React Native 应用程序时调用函数

转载 作者:行者123 更新时间:2023-12-02 16:50:23 24 4
gpt4 key购买 nike

我有一个使用此代码的应用程序:Linking.openURL('google.navigation:q='+latitude+'+'+longitude) 在我的 React Native Expo 应用程序中退出到谷歌地图.使用 Android(和 iOS),您可以使用手机的后退按钮返回到您之前的应用程序。我想知道当我的应用程序重新出现时如何调用函数。我的应用程序中有一些 GPS 数据,我想在他们返回我的应用程序后立即移动到某个位置后重新更新我的数据。我发现了这个....

componentWillMount() {
const { navigation } = this.props;
this.focusListener = navigation.addListener('didFocus', () => {
// The screen is focused
// Call any action
});
}

但是当我从 map 返回时它似乎没有调用..,

这很接近还是我做的完全错了?

谢谢

最佳答案

可以使用AppState来实现。

componentDidMount(){
AppState.addEventListener('change', this.handleAppStateChange);
}

handleAppStateChange = (nextAppState) => {
//the app from background to front
if (this.state.appState.match(/inactive|background/) && nextAppState === 'active') {

}
//save the appState
this.setState({ appState: nextAppState });
}

源代码注释说:

 *AppStateIOS can tell you if the app is in the foreground or background,
* and notify you when the state changes.
* * AppStateIOS is frequently used to determine the intent and proper
* behavior
* when handling push notifications.
* * iOS App States
* active - The app is running in the foreground
* background - The app is running in the background. The user is
*either in another app or on the home screen
* inactive - This is a transition state that currently never happens
* for typical React Native apps.

不同状态的含义:

  • active - 应用在前台运行

  • 后台 - 应用程序在后台运行。
    用户是:
    1、在另一个应用中
    2、在主屏幕上
    [Android] 在另一个 Activity 上(即使它是由您的应用启动的)

  • [iOS] inactive - 这是过渡时出现的状态在前景和背景之间,以及在不活动期间例如进入多任务 View 或在传入的情况下打电话

你必须小心处理 android 的状态。

关于reactjs - 返回 React Native 应用程序时调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58987051/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com