Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global variables keep value on restarting app #36624

Closed
euaxvision opened this issue Mar 24, 2023 · 2 comments
Closed

Global variables keep value on restarting app #36624

euaxvision opened this issue Mar 24, 2023 · 2 comments

Comments

@euaxvision
Copy link

euaxvision commented Mar 24, 2023

Description

I found that global variables are not reset in one of the following situations on Android:

  • changing system font size in Settings app (Settings > Display > Font Size and Styles), then app is restarted.
  • changing navigation style between 3-button navigation and gesture navigation in Settings app, then app is restarted.

I believe that componentDidMount is called when and only on app launched, by the fact that componentDidMount is called in <App> when the restart.
If it is right, it means that the code example in "Snack, code example, screenshot, or link to a repository" indicates global variables not reset on restarting app.

As a supplement, I found that the difference between first launch and restart.

  • When app is restarted, Application class is still alive, not re-created.
  • When app launches, both Application class and Activity class are created.

Though it might be a specification that global variables are only initialized when Application class is created but not reset when Activity class is created in Android, I cannot decide if it is a bug or not, therefore I will post this issue.

React Native Version

0.70.6

Output of npx react-native info

info Fetching system and libraries information...
System:
    OS: macOS 12.6
    CPU: (12) x64 Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
    Memory: 1.28 GB / 32.00 GB
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 16.13.1 - ~/.nodenv/versions/16.13.1/bin/node
    Yarn: 1.22.19 - ~/.nodenv/versions/16.13.1/bin/yarn
    npm: 8.1.2 - ~/.nodenv/versions/16.13.1/bin/npm
    Watchman: 2022.07.04.00 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.11.3 - /Users/*****/.rbenv/shims/pod
  SDKs:
    iOS SDK:
      Platforms: DriverKit 22.1, iOS 16.1, macOS 13.0, tvOS 16.1, watchOS 9.1
    Android SDK:
      Android NDK: 21.4.7075529
  IDEs:
    Android Studio: 2020.3 AI-203.7717.56.2031.7935034
    Xcode: 14.1/14B47b - /usr/bin/xcodebuild
  Languages:
    Java: 11.0.12 - /usr/bin/javac
  npmPackages:
    @react-native-community/cli: Not Found
    react: 18.1.0 => 18.1.0 
    react-native: 0.70.6 => 0.70.6 
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found
info React Native v0.71.4 is now available (your project is running on v0.70.6).
info Changelog: https://github.com/facebook/react-native/releases/tag/v0.71.4
info Diff: https://react-native-community.github.io/upgrade-helper/?from=0.70.6
info For more info, check out "https://reactnative.dev/docs/upgrading".

Steps to reproduce

This intstruction shows that a global variable is not reset when app is restarted by changing system font size.

  1. Create sample app.

      npx react-native init MyReactNativeApp --version 0.70.6
    
  2. Add code to App.js as the sample describes. it contains the procedures

  • declare a global variable initialized with false (globalVariable)
  • print the value of globalVariable to console log
  • assign true to globalVariable after printing log
  1. install app to an Android device with the following command and launch

    npx react-native run-android
    
  2. check console log (it is expected that the value should be false)

  3. change system font size in "Settings" app.

  4. set the app to foreground (the app seems to be restarted) then check console log (We are able to watch that the value is still true, not reset.)

Snack, code example, screenshot, or link to a repository

This is an excerpt (a global variable and App component) of App.js, adding step2 in 'Steps to reproduce.'

let globalVariable = false;

const App: () => Node = () => {
  console.log('globalVariable:' + globalVariable);
  globalVariable = true;

  const isDarkMode = useColorScheme() === 'dark';

  const backgroundStyle = {
    backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
  };

  return (
    <SafeAreaView style={backgroundStyle}>
      <StatusBar
        barStyle={isDarkMode ? 'light-content' : 'dark-content'}
        backgroundColor={backgroundStyle.backgroundColor}
      />
      <ScrollView
        contentInsetAdjustmentBehavior="automatic"
        style={backgroundStyle}>
        <Header />
        <View
          style={{
            backgroundColor: isDarkMode ? Colors.black : Colors.white,
          }}>
          <Section title="Step One">
            Edit <Text style={styles.highlight}>App.js</Text> to change this
            screen and then come back to see your edits.
          </Section>
          <Section title="See Your Changes">
            <ReloadInstructions />
          </Section>
          <Section title="Debug">
            <DebugInstructions />
          </Section>
          <Section title="Learn More">
            Read the docs to discover what to do next:
          </Section>
          <LearnMoreLinks />
        </View>
      </ScrollView>
    </SafeAreaView>
  );
};
@github-actions
Copy link

⚠️ Newer Version of React Native is Available!
ℹ️ You are on a supported minor version, but it looks like there's a newer patch available. Please upgrade to the highest patch for your minor or latest and verify if the issue persists (alternatively, create a new project and repro the issue in it). If it does not repro, please let us know so we can close out this issue. This helps us ensure we are looking at issues that still exist in the most recent releases.

@blakef
Copy link
Contributor

blakef commented Apr 4, 2023

I'm having to fill in a bunch of gaps here, but I'd expect that behaviour. There are two things happening here:

  1. your code is loaded into the VM, and
  2. the React runtime is mounting a component.

When you cold start your application (Activity) you get 1 & 2. Moving the app between some states (running → pause → running) may cause a component to remount but importantly it isn't doing 1 (which sets globalVariable to false).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants