-
Notifications
You must be signed in to change notification settings - Fork 24.4k
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
BackHandler and remote debug #15497
Comments
Any updates?? |
+1 |
Can you reproduce the issue using Snack? This step is necessary for people to be able to see and debug the issue being reported. |
@hramos I explain in details how to reproduce the issue , I cannot reproduce the issue using Snack because there is no dev menu in Snack , so |
You can run the Snack within the Expo Client and shake the device to trigger a menu to show up, no? In any case, this is a community project. To answer your "any updates?" question, we'd take a look at any PR that proposes a fix. |
@hramos , I tried before using Expo Client , there is no dev menu , You can try it yourself |
@sm2017 Is there a Symbol polyfill included by your app or any of your dependencies? If you do, than this could be caused by issue #11968, which I have a PR to fix #15182. Most desktop browsers support |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. If you think this issue should definitely remain open, please let us know why. Thank you for your contributions. |
Hi, I am having the same trouble with it. componentDidMount () { componentWillUnmount () { backAndroid () { |
@sm2017, have you resolved this issue? |
@howilliams No |
Same issue here, is there any workaround ? |
i met this issue too, when i open debugger,android back button works well to go back to the previous page,but when i close debugger or release a version ,press back button just lead to exit app.Pls help to solve this issue asap,thanks. What i do: version: |
@Victoriayangx Please upgrade to 0.51, your likely encountering a bug that has already been fixed. |
Hey, @dantman. It still doesn't work in normal mode, but it's working on 'Debug Only' mode and it's really really weird. I have spent 2 days to track the issues and found out that it's the same on version 0.51. I am gonna attach another screenshot when we run it in the 'Debug Mode'. As you can see, in the debug mode, the Set are converted well to Array var but that doesn't work in normal mode. |
HI, @sm2017. Have you found the round-about? |
Hi @sm2017, I am facing the same issue. I am using React Navigation. When the remote debug mode is on, the back handler works perfectly, |
I changed the source code in BackHandler.android.js,then it worked. Background: SourceCode:(react-native/Libraries/Utilities/BackHandler.android.js) _backPressSubscriptions.forEach(item=>subscriptions.push(item)); if (invokeDefault) { |
Bingo!!! This works. Can you fork the repo and submit a merge request so that the developers all over the world would get updated one. |
Hey Guys. For me, this issue for Back Handler was due to a react-native-pathjs-charts package that was being used. I created a new solution and added each package one by one and kept on testing. Path js package stopped the back handler. ES6 syntax distribution for that package worked for me. I copied that files and add that to my component and used it from there. Same package or some other package might have caused the same for you. |
Hi Navneet, Can you please explain me what exactly is to be done? I am using react-native-pathjs-charts in my project, so can you please explain me what is to be changed and where? |
@Victoriayangx Thanks a ton!!! 💃 you are my saviour |
@dantman , Recently I met another issue about duplicate remove in Array. So,I suspect BackHandler cause App exit maybe also caused by “new Set”. So in Our React-Native Project, to achieve remove duplicate element in Array , we will not use new Set until the issue is resolved. Hope it can help.Any advice,pls let me know.thanks. |
@Victoriayangx Is there any solution other than changing in the library because I am using Expo that won't allow me to make the change in the library. |
Hi, I created a PR which fixes this issue. Maybe you can verify this this change and give a thump up for #19077. |
This fix worked for me. #3223 (comment) |
This issue occurred for me when pulling in
For other people the issue could be happening because the polyfill is being imported from a dependency, might be able to override how |
still getting the problem and won't be able to change android folder because I'm using expo, please someone from RN, take one of the PRs here into consideration, really need it to work! :( |
@Victoriayangx You are the great buddy. Working like this in 54.2 var DeviceEventManager = require('NativeModules').DeviceEventManager; var DEVICE_BACK_EVENT = 'hardwareBackPress'; type BackPressEventName = $Enum<{ var _backPressSubscriptions = new Set(); RCTDeviceEventEmitter.addListener(DEVICE_BACK_EVENT, function () { // var subscriptions = Array.from(_backPressSubscriptions.values()).reverse(); if (invokeDefault) { |
Any updates on this? editing android folder shouldn't be the only way to fix this issue... it's unfair for CRNA/Expo users :$ |
@princepardeep isn't that code this workaround? #3223 (comment) |
I that have same issue, what is the solution? |
it was working fine in my case. change in this file Working like this in 54.2 var DeviceEventManager = require('NativeModules').DeviceEventManager; var DEVICE_BACK_EVENT = 'hardwareBackPress'; type BackPressEventName = $Enum<{ var _backPressSubscriptions = new Set(); RCTDeviceEventEmitter.addListener(DEVICE_BACK_EVENT, function () { // var subscriptions = Array.from(_backPressSubscriptions.values()).reverse(); if (invokeDefault) { |
@princepardeep this is the same code you already posted, and which is posted in #3223 (comment) ... you should avoid re-posting the same code. But yes, it is frustrating, that this kind of bug still exists. But instead of fixing that inside the core-file, I've added this code to my main app class: constructor(props){
super(props);
// https://github.com/facebook/react-native/issues/3223#issuecomment-355064410
this.backPressSubscriptions = new Set();
}
componentDidMount() {
if (Platform.OS === 'android') {
// there is a bug/workaround on android https://github.com/facebook/react-native/issues/3223#issuecomment-355064410
DeviceEventEmitter.removeAllListeners('hardwareBackPress');
DeviceEventEmitter.addListener('hardwareBackPress', () => {
let invokeDefault = true;
const subscriptions = [];
this.backPressSubscriptions.forEach(sub => subscriptions.push(sub));
for (let i = 0; i < subscriptions.reverse().length; i += 1) {
if (subscriptions[i]()) {
invokeDefault = false;
break;
}
}
if (invokeDefault) {
BackHandler.exitApp();
}
});
this.backPressSubscriptions.add(this.handleAndroidHardwareBack);
}
}
componentWillUnmount() {
if (Platform.OS === 'android') {
DeviceEventEmitter.removeAllListeners('hardwareBackPress');
this.backPressSubscriptions.clear();
}
} |
I find it unfeasible for me to create multiple listening events in different components |
There is still my 4 month old PR #19077 which fixes this... But nobody have the time to review/merge it. 😞 @TranHanHieu @princepardeep @FibreFoX Maybe you can verify if this change works for you as well and upvote the PR? |
oke, thanks you @jerolimov ! |
I created an HOC to handle android back button press, then just pass on a function I want to call... I don’t actually have a viewable component to it, I return null, and then just add it to the bottom of each component I need it. |
Summary: This fixes issues #18954 and #15497 The transformation from Set to (an reversed) Array does not work on Android devices when the remote debugging was started with `react-native run-android`. <!-- Required: Write your motivation here. If this PR fixes an issue, type "Fixes #issueNumber" to automatically close the issue when the PR is merged. --> Add the following code to your App.js and run the app with `react-native run-android` in an Android device: ``` componentDidMount() { BackHandler.addEventListener('hardwareBackPress', () => { console.warn('Callback called. Do not close app. Do nothing at all.'); return true; // Do not close the app. }); } ``` After adding this, the app should NOT close when you press the back button. Without this fix, the app closes (which is the default behaviour). See also the comments from Victoriayangx in #18954 and #15497. <!-- Does this PR require a documentation change? Create a PR at https://github.com/facebook/react-native-website and add a link to it here. --> <!-- Required. Help reviewers and the release process by writing your own release notes. See below for an example. --> [ANDROID] [BUGFIX] [BackHandler] - Fix that BackHandler was not called on Android <!-- **INTERNAL and MINOR tagged notes will not be included in the next version's final release notes.** CATEGORY [----------] TYPE [ CLI ] [-------------] LOCATION [ DOCS ] [ BREAKING ] [-------------] [ GENERAL ] [ BUGFIX ] [ {Component} ] [ INTERNAL ] [ ENHANCEMENT ] [ {Filename} ] [ IOS ] [ FEATURE ] [ {Directory} ] |-----------| [ ANDROID ] [ MINOR ] [ {Framework} ] - | {Message} | [----------] [-------------] [-------------] |-----------| EXAMPLES: [IOS] [BREAKING] [FlatList] - Change a thing that breaks other things [ANDROID] [BUGFIX] [TextInput] - Did a thing to TextInput [CLI] [FEATURE] [local-cli/info/info.js] - CLI easier to do things with [DOCS] [BUGFIX] [GettingStarted.md] - Accidentally a thing/word [GENERAL] [ENHANCEMENT] [Yoga] - Added new yoga thing/position [INTERNAL] [FEATURE] [./scripts] - Added thing to script that nobody will see --> Pull Request resolved: #19077 Reviewed By: yns88 Differential Revision: D9692298 Pulled By: hramos fbshipit-source-id: 4526b07a4924055ebc9c42e02615c27aa5c97fbb
Summary: This fixes issues facebook#18954 and facebook#15497 The transformation from Set to (an reversed) Array does not work on Android devices when the remote debugging was started with `react-native run-android`. <!-- Required: Write your motivation here. If this PR fixes an issue, type "Fixes #issueNumber" to automatically close the issue when the PR is merged. --> Add the following code to your App.js and run the app with `react-native run-android` in an Android device: ``` componentDidMount() { BackHandler.addEventListener('hardwareBackPress', () => { console.warn('Callback called. Do not close app. Do nothing at all.'); return true; // Do not close the app. }); } ``` After adding this, the app should NOT close when you press the back button. Without this fix, the app closes (which is the default behaviour). See also the comments from Victoriayangx in facebook#18954 and facebook#15497. <!-- Does this PR require a documentation change? Create a PR at https://github.com/facebook/react-native-website and add a link to it here. --> <!-- Required. Help reviewers and the release process by writing your own release notes. See below for an example. --> [ANDROID] [BUGFIX] [BackHandler] - Fix that BackHandler was not called on Android <!-- **INTERNAL and MINOR tagged notes will not be included in the next version's final release notes.** CATEGORY [----------] TYPE [ CLI ] [-------------] LOCATION [ DOCS ] [ BREAKING ] [-------------] [ GENERAL ] [ BUGFIX ] [ {Component} ] [ INTERNAL ] [ ENHANCEMENT ] [ {Filename} ] [ IOS ] [ FEATURE ] [ {Directory} ] |-----------| [ ANDROID ] [ MINOR ] [ {Framework} ] - | {Message} | [----------] [-------------] [-------------] |-----------| EXAMPLES: [IOS] [BREAKING] [FlatList] - Change a thing that breaks other things [ANDROID] [BUGFIX] [TextInput] - Did a thing to TextInput [CLI] [FEATURE] [local-cli/info/info.js] - CLI easier to do things with [DOCS] [BUGFIX] [GettingStarted.md] - Accidentally a thing/word [GENERAL] [ENHANCEMENT] [Yoga] - Added new yoga thing/position [INTERNAL] [FEATURE] [./scripts] - Added thing to script that nobody will see --> Pull Request resolved: facebook#19077 Reviewed By: yns88 Differential Revision: D9692298 Pulled By: hramos fbshipit-source-id: 4526b07a4924055ebc9c42e02615c27aa5c97fbb
PR #19077 is merged and is finally part of the latest RN release 0.58+ I hope for all of you that this issue here is fixed when you upgrade your RN version. 😄 |
Awesome! I'm going to close this for the time being, if the issue is still present please submit a new issue :) Thanks @jerolimov! |
Is this a bug report?
(Yes)
Have you read the Contributing Guidelines?
(Yes)
Environment
react-native -v
:node -v
:npm -v
:yarn --version
:Then, specify:
react-native run-android
Steps to Reproduce
(Write your steps here:)
If you run the code in
development
environment , it don't works and when you press back button , app will be closed , but indevelopment
environment if you turn onremote debug
, the code works well and app not closedExpected Behavior
(Prevent closing app on back button pressed)
Actual Behavior
(On
development
environment whenremote debug
is off it don't woks but whenremote debug
is on it works)Reproducible Demo
(N/A)
The text was updated successfully, but these errors were encountered: