0

我正在使用 Modal 在我的应用程序中打开条款。在这里,我可以毫无问题地打开和关闭模式。

但是,如果我打开模式一次,然后在应用程序内不做任何事情,它就会进入睡眠模式。然后在手机上,它会显示应用程序,我无法看到模态窗口,如果我再次尝试打开模态窗口它没有响应?

任何解决方法?

4

1 回答 1

1

有一个想法,你可以尝试使用Appstate来处理它。

检测应用程序进入前台,然后关闭模态,也许它可以工作?

这是文档上的应用程序状态(功能)示例,在控制台行关闭模式。(如果您使用类组件,则与文档类示例中的理论使用相同)

import React, { useEffect, useState } from "react";
import { AppState, StyleSheet, Text, View } from "react-native";

const AppStateExample = () => {
  const [appState, setAppState] = useState(AppState.currentState);

  useEffect(() => {
    AppState.addEventListener("change", _handleAppStateChange);

    return () => {
      AppState.removeEventListener("change", _handleAppStateChange);
    };
  }, []);

  const _handleAppStateChange = nextAppState => {
    if (appState.match(/inactive|background/) && nextAppState === "active") {
      console.log("App has come to the foreground!");  // Close the Modal here 
    }
    setAppState(nextAppState);
  };

  return (
    <View style={styles.container}>
      <Text>Current state is: {appState}</Text>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center"
  }
});

export default AppStateExample;

试一试。

于 2020-04-29T05:57:46.763 回答