Skip to content

[ANDROID] [FIXED] - Fix BackHandle callback undefined cause crash issue#48388

Closed
ISeekTruth wants to merge 2 commits intofacebook:mainfrom
ISeekTruth:patch-1
Closed

[ANDROID] [FIXED] - Fix BackHandle callback undefined cause crash issue#48388
ISeekTruth wants to merge 2 commits intofacebook:mainfrom
ISeekTruth:patch-1

Conversation

@ISeekTruth
Copy link
Copy Markdown
Contributor

@ISeekTruth ISeekTruth commented Dec 25, 2024

Summary:

Error message:

we got an error

t[n] is not a function. (In 't[n]()', 't[n]' is undefined) \n <unknown> (index.bundle:317:168:317)

it related the BackHandle execute handle function.

Investigation result

our project has screen filesApp.tsx, Dashboard.tsx, and Profile.tsx.
When launching the app, the screen order is App.tsx -> Dashboard.tsx, then user can switch to Profile.tsx
For App.tsx and Dashboard.tsx, we just prevent the hardware button action use usePreventHardwareBackPressEffect() in the first line of screen code.

export const useHardwareBackPressEffect = (goBack?: () => boolean): void => {
  useEffect(() => {
    if (goBack) {
      BackHandler.addEventListener("hardwareBackPress", goBack);
      return () => {
        BackHandler.removeEventListener("hardwareBackPress", goBack);
      };
    }
    return undefined;
  }, [ goBack ]);
};
export const usePreventHardwareBackPressEffect = (): void => useHardwareBackPressEffect(() => true);

currently, _backPressSubscriptions has 2 callback functions.
then user switch to Profile.tsx screen, and has the below code for hardwareback button and the second doesn't return true:

// first one
usePreventHardwareBackPressEffect();
...

// second one
useEffect(() => {
  const backButtonListener = BackHandler.addEventListener(
    "hardwareBackPress",
    () => navigate(Navigation.Login);
  );
  return () => backButtonListener.remove();
});

currently, _backPressSubscriptions has 4 callback functions, include previous 2 and new 2 of Profile.tsx.
When the user press hardwareback button, it will navigate to the login screen, so the issue occurs:
the latest callback will be executed first, then the navigation will let the screen unmount, which will destroy the effect, so the code removing 2 hardwareBackPress callback of Profile.tsx by executed

return () => {
        BackHandler.removeEventListener("hardwareBackPress", goBack);
      };

After the navigation ends and the loop is restored, the init i is 3, then i--, i is 2, then _backPressSubscriptions[2] is undefined now and executes as a function, so the app crashes.

for (let i = _backPressSubscriptions.length - 1; i >= 0; i--) {
    if (_backPressSubscriptions[i]()) {
      return;
    }
  }

that's the issue I met.

Changelog:

[ANDROID] [FIXED] - Fix BackHandle callback undefined cause crash issue

Test Plan:

BleemIs42 added 2 commits April 24, 2024 22:03
we got an error of `t[n] is not a function. (In 't[n]()', 't[n]' is undefined) \n <unknown> (index.bundle:317:168:317)`, it related the `BackHandle` execute handle function
@facebook-github-bot
Copy link
Copy Markdown
Contributor

Hi @BleemIs42!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@facebook-github-bot
Copy link
Copy Markdown
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. labels Dec 25, 2024
Comment thread packages/react-native/Libraries/Utilities/BackHandler.android.js
@facebook-github-bot
Copy link
Copy Markdown
Contributor

@rshest has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label Dec 27, 2024
@facebook-github-bot
Copy link
Copy Markdown
Contributor

@rshest merged this pull request in 44705fe.

@react-native-bot
Copy link
Copy Markdown
Collaborator

This pull request was successfully merged by @BleemIs42 in 44705fe

When will my fix make it into a release? | How to file a pick request?

@ISeekTruth ISeekTruth deleted the patch-1 branch December 28, 2024 13:38
rshest added a commit to rshest/react-native that referenced this pull request Dec 30, 2024
Summary:
## Changelog:
[Internal] - 

Floow-up to facebook#48388, based on the diff discussion (D67648077).

Adds a bit better typing to `BackHandler.js`.

Differential Revision: D67713236
facebook-github-bot pushed a commit that referenced this pull request Dec 30, 2024
Summary:
Pull Request resolved: #48411

## Changelog:
[Internal] -

Floow-up to #48388, based on the diff discussion (D67648077).

Adds a bit better typing to `BackHandler.js`.

Reviewed By: blakef

Differential Revision: D67713236

fbshipit-source-id: 95435898d8ea87f6ae32a6db859d6641e1264972
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants