我试图在用户离开应用程序之前更改UI(用户处于多任务视图或切换到其他应用程序).要在用户离开应用时更具体,我想添加带有应用徽标的全屏视图.
我正在使用AppState.
在iOS上它按预期工作:在多任务处理视图应用程序变为非活动状态,一旦切换到其他应用程序状态转到后台.当状态为非活动状态时,我仍然可以更改UI.
但是,在Android上,状态是活动状态还是后台状态.
问题是在后台状态我不能再改变UI了.
这是Android上的错误吗?如果没有,我有什么选择让它在Android上运行.
谢谢.
解决方法
如果需要,可以通过向MainActivity.java添加一些代码来监听其生命周期事件,从而模拟与iOS相同的状态
- //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监听这些生命周期更改
- const nativeEventListener = DeviceEventEmitter.addListener('ActivityStateChange',(e)=>{
- console.log(e.event);
- })