反应原生 AppState 在 Android 上没有“非活动"状态 [英] react native AppState has no 'inactive' state on Android

查看:18
本文介绍了反应原生 AppState 在 Android 上没有“非活动"状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在用户离开应用程序之前更改 UI(用户处于多任务视图或切换到其他应用程序).更具体地说,当用户离开应用时,我想添加带有应用徽标的全屏视图.

I am trying to change the UI just before the user leaves the app (user is in multi-tasking view or switches to some other app). To be more specific when user leaves the app I want to add a fullscreen view with the app logo.

我为此使用了 AppState.

I am using AppState for that.

在 iOS 上它按预期工作:在多任务视图中应用程序处于非活动状态,一旦切换到其他应用程序状态就会进入后台.当状态处于非活动状态时,我仍然可以更改 UI.

On iOS it works as expected: in multitasking view app gets inactive state and once switched to other app state goes to background. When state is inactive I can still change UI.

然而,在 Android 上,状态要么是活动的,要么是后台的.问题是在后台状态下,我无法再更改 UI.

However, on Android the state is either active or background. Problem is that in background state I cannot change the UI anymore.

这是 Android 上的错误吗?如果没有,我有什么选择可以让它在 Android 上运行.

Is this a bug on Android? If not, what are my options to get it working on Android.

谢谢.

推荐答案

如果需要,您可以通过在 MainActivity.java 中添加一些代码来监听其生命周期事件,从而模拟与 iOS 相同的状态

If you need to, you can simulate the same states as iOS by adding some code to MainActivity.java to listen to its lifecycle events

//onResume = 'active'  
//onPause = 'inactive'  
//onStop = 'background' 

@Override
public void onResume() {
    super.onResume();
    ReactContext reactContext = getReactInstanceManager().getCurrentReactContext();
    WritableMap params = Arguments.createMap();
    params.putString("event", "active");

    // when app starts reactContext will be null initially until bridge between Native and React Native is established
    if(reactContext != null) {
        getReactInstanceManager().getCurrentReactContext()
            .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
            .emit("ActivityStateChange", params);
    }
}

@Override
public void onPause() {
    super.onPause();
    ReactContext reactContext = getReactInstanceManager().getCurrentReactContext();
    WritableMap params = Arguments.createMap();
    params.putString("event", "inactive");

    if(reactContext != null) {
        getReactInstanceManager().getCurrentReactContext()
                .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                .emit("ActivityStateChange", params);
    }
}

@Override
public void onStop() {
    super.onStop();
    ReactContext reactContext = getReactInstanceManager().getCurrentReactContext();
    WritableMap params = Arguments.createMap();
    params.putString("event", "background");

    if(reactContext != null) {
        getReactInstanceManager().getCurrentReactContext()
                .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                .emit("ActivityStateChange", params);
    }
}

然后在你的 JS 中使用 DeviceEventEmitter 监听这些生命周期变化

Then in your JS listen to these lifecycle changes using DeviceEventEmitter

const nativeEventListener = DeviceEventEmitter.addListener('ActivityStateChange',
  (e)=>{
      console.log(e.event);
})

这篇关于反应原生 AppState 在 Android 上没有“非活动"状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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