-
-
Notifications
You must be signed in to change notification settings - Fork 5k
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
Trouble handling android hardware back button #1819
Comments
I've set up an app that seems to be working. Handle all I am using redux and I use the
I haven't had any problems yet, but no one knows what the future will bring :) |
Will try it out @Selman555 👍 |
Try this,
|
thanx @igofind |
@Selman555 when use structure
in my |
Selman555's code there worked beautiful for me. I haven't sampled any wild edge cases, but it definitely works great. I recommend trying it and do not edit anything. I also haven't researched yet but Here is my App.js file using it: import React, { Component } from 'react'
import { Platform, BackHandler } from 'react-native'
import { Provider, connect } from 'react-redux'
import { addNavigationHelpers } from 'react-navigation'
import { NavigationStack } from './navigation/nav_reducer'
import store from './store'
class App extends Component {
componentWillMount() {
if (Platform.OS !== 'android') return
BackHandler.addEventListener('hardwareBackPress', () => {
const { dispatch, nav } = this.props
if (nav.routes.length === 1 && (nav.routes[0].routeName === 'Login' || nav.routes[0].routeName === 'Start')) return false
dispatch({ type: 'Navigation/BACK' })
return true
})
}
componentWillUnmount() {
if (Platform.OS === 'android') BackHandler.removeEventListener('hardwareBackPress')
}
render() {
const { dispatch, nav } = this.props
const navigation = addNavigationHelpers({
dispatch,
state: nav
})
return <NavigationStack navigation={navigation} />
}
}
const mapStateToProps = ({ nav }) => ({ nav })
const RootNavigationStack = connect(mapStateToProps)(App)
const Root = () => (
<Provider store={store}>
<RootNavigationStack />
</Provider>
)
export default Root |
@Sean-Snow Make sure that you haven't created a second back event handler somewhere else that cancels the root back event. I'm not 100% sure about this, but if you define another handler that returns false, your root event handler won't be handled as you might think. |
Pinging OP @Suresh-R-S since this issue has not been active for a while, and it's related to an old version of the lib (moreover solution provided by @Selman555 & @agm1984 seems to fulfill the issue) . Please let me know if you want this to remain open, because if I get no answer in the next 7 days I will close it. |
Hi there @Suresh-R-S , In an effort to cleanup this project and prioritize a bit, since this issue had no follow up since my last comment I'm going to close it. If you are still having the issue with the latest version (especially if it's a bug report) please check the current open issues, and if you can't find a duplicate, open a new one that uses the new Issue Template to provide some more details to help us solve it. |
@Suresh-R-S even I wanted to handle the back press for different screens in a custom manner. Can you help me out here please? |
@Selman555 code with me to come cannot read property index of undefined -> related to createNavigationContainer
|
this may resolve issues that folks encountered here: b3bf806 if not, please create a new issue with an example on snack.expo.io or github, thanks! |
Here is the code constructor(props) { componentWillUnmount() {
This code works. I used it |
In my case I was using a stack navigator so I had to get the stack first and then check its routes.
|
How to use it in react navigation v2 :( |
Is there a way we can configure this for rect-navigation v2 & without redux integration? |
How to use in react-native-router-flux |
Suppose i am on initial screen on stackNavigator, When i press back it shows Alert message and not waits for user to press ok or cancel..Anyone tell me what should i do to pause exit function. |
Show the alert popup in a timeout and add return true; after that.
|
I have a stack navigator. Inside the stack navigator I have drawerNavigator. I added the backhandler listener in App.js. It get called in Stacknavigator. But not in inner pages of DrawerNavigator. I would like to make backHandler a common function. How can I acheive this?
|
I've resolved this by using this.props.navigation.isFocused() function Example: componentDidMount() { handleBackPress = () => { |
please check this hook: https://github.com/react-navigation/hooks#usefocuseffectcallback
|
can anyone tell me how to use software back press key in react native in back handling button? |
Do you mean Android's onscreen back button, IOS's swipe gesture? If you're talking about a custom made back button, the button's |
Hi,
I'am having trouble handling the android hardware back button.
If I use react-native BackHandler and add a listener in each of my screens, then I'am unable to remove the listener when the screen changes (I believe it is because I'am using StackNavigator & componentWillUnmount is not fired).
So, how to handle android hardware back button?
RN Version 0.44.0
react-navigation Version 1.0.0-beta.9
redux Version 3.6.0
The text was updated successfully, but these errors were encountered: