本机AppState在Android上没有“无效”状态

前端之家收集整理的这篇文章主要介绍了本机AppState在Android上没有“无效”状态前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在用户离开应用程序之前更改UI(用户处于多任务视图或切换到其他应用程序).要在用户离开应用时更具体,我想添加带有应用徽标的全屏视图.

我正在使用AppState.

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

但是,在Android上,状态是活动状态还是后台状态.
问题是在后台状态我不能再改变UI了.

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

谢谢.

解决方法

如果需要,可以通过向MainActivity.java添加一些代码来监听其生命周期事件,从而模拟与iOS相同的状态
  1. //onResume = 'active'
  2. //onPause = 'inactive'
  3. //onStop = 'background'
  4.  
  5. @Override
  6. public void onResume() {
  7. super.onResume();
  8. ReactContext reactContext = getReactInstanceManager().getCurrentReactContext();
  9. WritableMap params = Arguments.createMap();
  10. params.putString("event","active");
  11.  
  12. // when app starts reactContext will be null initially until bridge between Native and React Native is established
  13. if(reactContext != null) {
  14. getReactInstanceManager().getCurrentReactContext()
  15. .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
  16. .emit("ActivityStateChange",params);
  17. }
  18. }
  19.  
  20. @Override
  21. public void onPause() {
  22. super.onPause();
  23. ReactContext reactContext = getReactInstanceManager().getCurrentReactContext();
  24. WritableMap params = Arguments.createMap();
  25. params.putString("event","inactive");
  26.  
  27. if(reactContext != null) {
  28. getReactInstanceManager().getCurrentReactContext()
  29. .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
  30. .emit("ActivityStateChange",params);
  31. }
  32. }
  33.  
  34. @Override
  35. public void onStop() {
  36. super.onStop();
  37. ReactContext reactContext = getReactInstanceManager().getCurrentReactContext();
  38. WritableMap params = Arguments.createMap();
  39. params.putString("event","background");
  40.  
  41. if(reactContext != null) {
  42. getReactInstanceManager().getCurrentReactContext()
  43. .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
  44. .emit("ActivityStateChange",params);
  45. }
  46. }

然后在您的JS中使用DeviceEventEmitter监听这些生命周期更改

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

猜你在找的Android相关文章