react-native icon indicating copy to clipboard operation
react-native copied to clipboard

[Android][0.22.2] Possible Unhandled Promise Rejection after reopening app from minimized/background state

Open jorgemichael024 opened this issue 10 years ago • 26 comments

React Native Version: 0.22.2 Platform: Android System: Ubuntu 15.04 Device: Samsung Note 3 (5.0)

Hi guys, after upgrading from 0.21 to 0.22.2, I'm experiencing the warning below every time I reopen my app from minimized/background state.

'Possible Unhandled Promise Rejection (id: 0): Tried to change the status bar while not attached to an Activity'

But when I remove the StatusBar component from my view, the warning goes away (obviously).

This is my snippet:

<View style={ {flex: 1, backgroundColor: 'white'} }>
<StatusBar
  backgroundColor={this.props.statusbarBGColor}
          barStyle={"light-content"}
         />
        {toolbar}
    <View style={[styles.container, this.props.overrideStyle]}>
        {this.props.children}
    </View>            
</View>

jorgemichael024 avatar Mar 29 '16 04:03 jorgemichael024

Is there anything which re-renders the component in which you've the statusbar? e.g. - some event which updates state in parent component.

cc @janicduplessis

satya164 avatar Mar 29 '16 05:03 satya164

Do you use AppState and setState when the app state changes?

Also the warning should not happen in 0.23.0-rc1 as I've changed the implementation to be smarter and not update the status bar values if they didn't change.

janicduplessis avatar Mar 29 '16 06:03 janicduplessis

@satya164 , yes. The snippet is my BaseContainer which is used by Home screen that updates a lot. I'm also using AppState in my Home screen.

cc @janicduplessis

jorgemichael024 avatar Mar 29 '16 07:03 jorgemichael024

OS: Ubuntu 15.10 Emulator: Genymotions. React: 0.14.8 React-native: 0.22.2

I have this issue with the emulator Geny motions. When I'm saving the file it will say "Hot loading..." and give me the same error below. I would like to give a log, but no idea where the log is saved from either genymotion or react-native.

Any specific code this happens? no, it happens with any code.

cablegunmaster avatar Apr 06 '16 14:04 cablegunmaster

I am having the same issue, but for me it happens only on device, not on emulator (I am using the new standard emulator from build tools 25.1.1 rather than GenyMotion emulator, so that may be related). Whenever I make a change in my editor and cause a Hot Reload on device (Samsung Galaxy S7, haven't tested on other devices yet), I get the Promise Rejection warning. Output from logcat is:

04-11 16:28:56.384  1175  1507 I ReactNativeJS: [React Transform HMR] Patching ReactTest
04-11 16:28:56.504  1175  1507 W ReactNativeJS: Possible Unhandled Promise Rejection (id: 7):
04-11 16:28:56.504  1175  1507 W ReactNativeJS: Network request failed
04-11 16:28:56.504  1175  1507 W ReactNativeJS: onload@http://10.20.45.97:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:14486:21
04-11 16:28:56.504  1175  1507 W ReactNativeJS: _sendLoad@http://10.20.45.97:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:14079:7
04-11 16:28:56.504  1175  1507 W ReactNativeJS: setReadyState@http://10.20.45.97:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:14069:15
04-11 16:28:56.504  1175  1507 W ReactNativeJS: _didCompleteResponse@http://10.20.45.97:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:13964:19
04-11 16:28:56.504  1175  1507 W ReactNativeJS: call@[native code]
04-11 16:28:56.504  1175  1507 W ReactNativeJS: http://10.20.45.97:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:13902:110
04-11 16:28:56.504  1175  1507 W ReactNativeJS: emit@http://10.20.45.97:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:9481:28
04-11 16:28:56.504  1175  1507 W ReactNativeJS: __callFunction@http://10.20.45.97:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:7817:28
04-11 16:28:56.504  1175  1507 W ReactNativeJS: http://10.20.45.97:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:7721:22
04-11 16:28:56.504  1175  1507 W ReactNativeJS: guard@http://10.20.45.97:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:7675:3
04-11 16:28:56.504  1175  1507 W ReactNativeJS: callFunctionReturnFlushedQueue@http://10.20.45.97:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:7720:6
04-11 16:28:56.504  1175  1507 W ReactNativeJS: callFunctionReturnFlushedQueue@[native code]
04-11 16:28:56.504  1175  1507 W ReactNativeJS: global code

arild-jacobsen avatar Apr 11 '16 14:04 arild-jacobsen

I saw this warning and in my case it was because I was calling StatusBar.setTranslucent(true) before my main App component was actually mounted. (not sure if relevant to OP's problem since I am using the static method rather than the component).

cpsubrian avatar Jul 26 '16 21:07 cpsubrian

I got the same warning occasional on startup. (react-native: 0.28.0, react: 15.0.0, Android):

08-19 14:40:09.528 20791-21076/com.bee W/ReactNativeJS: Possible Unhandled Promise Rejection (id: 0):
                                                        Tried to change the status bar while not attached to an Activity
                                                        createErrorFromErrorData@http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:4698:20
                                                        http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:4649:35
                                                        __invokeCallback@http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:4515:15
                                                        http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:4401:24
                                                        guard@http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:4343:3
                                                        invokeCallbackAndReturnFlushedQueue@http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:4400:6
                                                        invokeCallbackAndReturnFlushedQueue@[native code]
                                                        global code

I found the rejection in the native implementation:

@ReactMethod
public void setHidden(final boolean hidden, final Promise res) {
  final Activity activity = getCurrentActivity();
  if (activity == null) {
    // res.reject("E_NO_ACTIVITY", "Tried to change the status bar while not attached to an Activity");
    res.reject(ERROR_NO_ACTIVITY, ERROR_NO_ACTIVITY_MESSAGE);
    return;
  }
  // ...
}

However, the rejection is not handled in the JavaScript implementation:

// react-native/Libraries/Components/StatusBar/StatusBar.js

const StatusBarManager = require('NativeModules').StatusBarManager;

setHidden(hidden: boolean, animation?: StatusBarAnimation) {
  animation = animation || 'none';
  StatusBar._defaultProps.hidden.value = hidden;
  if (Platform.OS === 'ios') {
    StatusBarManager.setHidden(hidden, animation);
  } else if (Platform.OS === 'android') {
    StatusBarManager.setHidden(hidden);
  }
},

Is there any way to pend the setXXX call till the value which getCurrentActivity returns is not null?

Sorry for my broken English.

nilptr avatar Aug 20 '16 08:08 nilptr

I think this happens methods on statusbar before react-native is attached to an activity. The best way to avoid this is either to use the component or call methods in componentDidMount of your main component.

We should probably improve the error message to make this less confusing and maybe catch the promise to avoid the unhandled promise rejection.

janicduplessis avatar Aug 20 '16 17:08 janicduplessis

It seems that componentDidMount of the main component may happen before react-native is attached to an activity.

class RNTest extends Component {
  componentDidMount() {
    StatusBar.setHidden(true);
  }

  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to React Native!
        </Text>
      </View>
    );
  }
}

// ...

AppRegistry.registerComponent('RNTest', () => RNTest);

screenshot_2016-08-23-19-31-11_com rntest

nilptr avatar Aug 25 '16 03:08 nilptr

+1, v0.32

I get the same WARNING message on device when the app is starting and then make it background by press HOME key. After switch to the app, the StatusBar has no effect and I'm running to the same issue.

Why there should be a WARNING messege? If it can't be setHidden, so it is. I think taking away the reject message makes sense

smallpath avatar Sep 04 '16 06:09 smallpath

+1, v0.33 same problem as @Smallpath

alzuma avatar Sep 18 '16 10:09 alzuma

+1 RN.0.34.0.RC.0

alexHlebnikov avatar Sep 19 '16 06:09 alexHlebnikov

+1RN.0.34.0

nodegin avatar Sep 24 '16 09:09 nodegin

+1 RN 0.34.1

Richard-Cao avatar Oct 08 '16 04:10 Richard-Cao

hey guys, how did you work around this issue?

antoinerousseau avatar Oct 17 '16 16:10 antoinerousseau

#10421 should fix this, the warning will be moved to a native log.

janicduplessis avatar Oct 17 '16 17:10 janicduplessis

Fresh 0.36 too. Is it possible that it is related to router-flux somehow? Or you don't use it?

bezenson avatar Oct 26 '16 06:10 bezenson

+1 RN 0.36.1

WatcherDing avatar Nov 03 '16 14:11 WatcherDing

This also happens to me and when I reopen the app the status bar is permanently hidden :/

marcshilling avatar Nov 10 '16 20:11 marcshilling

Getting this with RN 0.37 (Android, Windows) on emulator (have not tried an actual device) when I return to the app from a background state, but only when I exit the app with the Android hardware back button.

All I'm doing is:

<StatusBar
    backgroundColor='#FF8B8B'
    barStyle='light-content'
/>

and:

componentDidMount() {
    AppState.addEventListener('change', this._handleAppState);
}

componentWillUnmount() {
    AppState.removeEventListener('change', this._handleAppState);
}

_handleAppState(appState) {
    console.log('Application state change: '+appState);
}

mattisx avatar Nov 13 '16 19:11 mattisx