Cro*_*ile 6 javascript android react-native
我将尝试解释我的情况:
我有 2 个页面/屏幕
屏幕A.js:
...
<FlatList>
</FlatList>
...
Run Code Online (Sandbox Code Playgroud)
ScreenB.js:
...
<Button> This button for back to ScreenA </Button>
...
Run Code Online (Sandbox Code Playgroud)
我想要实现的目标:
当我按下 screenB 中的后退按钮时,我的应用程序将返回到 screenA。当我回到 screenA 时,我需要刷新 FlatList。(每次我回到屏幕A)。
我如何在 Android Native 中实现这种情况?
我添加此部分是为了帮助您更好地了解我面临的挑战。在 Android Native 中,我将使用 onResume 刷新列表(每次调用屏幕/页面时都会调用 onResume)。
我已经尝试过但现在对我不起作用:
我已经尝试过这个,但是当应用程序在后台然后在前台显示时我得到的只是调用:
state = {
appState: AppState.currentState
}
componentDidMount() {
AppState.addEventListener('change', this._handleAppStateChange);
}
componentWillUnmount() {
AppState.removeEventListener('change', this._handleAppStateChange);
}
_handleAppStateChange = (nextAppState) => {
if (this.state.appState.match(/inactive|background/) && nextAppState === 'active') {
console.log('App has come to the foreground!')
//i place the code here for refresh the flatList. But not work
}
this.setState({appState: nextAppState});
}
Run Code Online (Sandbox Code Playgroud)
navigation.addListener每次页面收到导航焦点时添加以触发。
举个例子:
class Page1 extends React.Component {
constructor(props) {
super(props);
this.state = { ts: 0 }
// this will fire every time Page 1 receives navigation focus
this.props.navigation.addListener('willFocus', () => {
this.setState({ ts: Date.now() })
})
}
render() {
return (
<View style={styles.container}>
<Text style={styles.paragraph}>
Page 1 [{this.state.ts}]
</Text>
<Button title="Page 2" onPress={() => this.props.navigation.navigate('Page2')}/>
</View>
);
}
}
Run Code Online (Sandbox Code Playgroud)
有关完整示例,请在手机上查看以下小吃。每次您导航回第 1 页时,时间戳都会更新,但应用程序恢复时不会更新。
您应该能够从此示例中根据您的需要扩展它。
| 归档时间: |
|
| 查看次数: |
4448 次 |
| 最近记录: |