title: Store timestamp when the app is in background. React native
body: <p>EDITED: I want to store a timestamp when the react native app goes into the background, to see if re-login is needed. I use the async storage to save the timestamp on the device, with help of the appstate functionality. 
  But the getItem and setItem code never executes. any idea why ?</p>
  
  <pre><code>componentDidMount() {
      AppState.addEventListener('change', this._handleAppStateChange);
  }
   _handleAppStateChange = (nextAppState: any) =&gt; {
  
      if (
          this.state.appState.match(/inactive|background/) &amp;&amp;
          nextAppState === 'active'
      ) {
          this.getTime();
          this.checkTimeStamp();
          console.log(this.state.time, 'time var');
          console.log('app is active, and has been in the background');
      } else if (
          this.state.appState == 'active' &amp;&amp;
          nextAppState == 'active'
      ) {
          this.setTime();
          this.checkTimeStamp();
          console.log(this.state.time, 'time var');
          console.log('app is active, and just opened.');
      } else {
          this.setTime()
              //sets the timestamp
          console.log('app in background or closed');
      }
      this.setState({ appState: nextAppState });
  };
  
  async setTime() {
      let seconds = this.generateTime();
      try {
          const val = await AsyncStorage.setItem('time', seconds.toString());
          this.setState({ time: Number(val) });
  
      } catch (error) {
          console.error('onRejected function called: ' + error);
      }
  }
  
  async getTime() {
      let please = await AsyncStorage.getItem('time');
      this.setState({ time: Number(please) });
      console.log(this.state.time, 'time var');
  </code></pre>
  
  <p>}</p>