You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用expo-splash-screen的preventAutoHideAsync出现Current activity not found报错求助

解决expo-splash-screen调用preventAutoHideAsync()报错问题

问题分析

你遇到的Current activity not found警告,核心原因是App组件初始化阶段,原生Activity还未完全就绪就调用了preventAutoHideAsync(),生产构建或Expo Go环境的启动生命周期时序和本地调试存在差异,导致方法调用时机不匹配。

解决方案

1. 延迟到组件挂载后调用

不要在App.js最顶部直接执行,改用useEffect钩子在组件挂载完成后调用:

import { useEffect } from 'react';
import * as SplashScreen from 'expo-splash-screen';

export default function App() {
  useEffect(() => {
    const initApp = async () => {
      try {
        await SplashScreen.preventAutoHideAsync();
        // 这里放置你的初始化逻辑,比如加载数据、初始化状态等
      } catch (err) {
        console.warn(err);
      }
    };

    initApp();
  }, []);

  // 其他组件逻辑
}

2. 结合AppState确保前台状态调用

通过监听App状态,只在App进入活跃状态时执行方法:

import { useEffect, useState } from 'react';
import { AppState } from 'react-native';
import * as SplashScreen from 'expo-splash-screen';

export default function App() {
  const [appState, setAppState] = useState(AppState.currentState);

  useEffect(() => {
    const handleStateChange = async (nextState) => {
      if (nextState === 'active' && appState !== 'active') {
        try {
          await SplashScreen.preventAutoHideAsync();
        } catch (err) {
          console.warn(err);
        }
      }
      setAppState(nextState);
    };

    const subscription = AppState.addEventListener('change', handleStateChange);
    return () => subscription.remove();
  }, [appState]);

  // 其他组件逻辑
}

3. 确保依赖版本兼容

运行expo install expo-splash-screen,让Expo自动安装适配当前v46 SDK的版本,避免版本不兼容导致的时序问题。

4. 检查生产构建的启动屏配置

确认app.json中的splash配置正确,示例如下:

{
  "expo": {
    "splash": {
      "image": "./assets/splash.png",
      "resizeMode": "contain",
      "backgroundColor": "#ffffff",
      "android": {
        "enableRemoteDebugging": false
      },
      "ios": {
        "supportsTablet": true
      }
    }
  }
}

关键注意事项

  • 禁止在组件渲染前的同步代码中调用preventAutoHideAsync(),必须放在异步生命周期钩子或状态更新后执行。
  • 完成初始化后,务必调用SplashScreen.hideAsync()隐藏启动屏,避免启动屏持续显示。

内容的提问来源于stack exchange,提问作者Kirti

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 00:55:13