Trying to Exit App on 'backButtonPressEvent' but not Working as Expected .
componentDidMount(){
BackAndroid.addEventListener('hardwareBackPress', function() {
BackAndroid.exitApp()
return false (tried with return true also)
})
}
render(){
var navigationView = (
<Menu closeDrawer={this.closeDrawer} {...this.props}/>
) ;
return (
<DrawerLayoutAndroid
ref={(_drawer) => this.drawer = _drawer}
drawerWidth={270}
drawerPosition={DrawerLayoutAndroid.positions.Left}
renderNavigationView={() => navigationView}>
<Nav {...this.props} openDrawer={this.openDrawer} closeDrawer={this.closeDrawer}/>
</DrawerLayoutAndroid>
)
}
}
This event Only fire once in the Component . Within this component there's DrawerLayout which have a
Scrollable-Tab-View component (Nav) . If I dont navigate to any tabs after mount , it works but after that the event doesn't get fired . Am I doing something wrong ? I dont know its the correct way call BackAndroid.exitApp() .
@PARAGJYOTI exitApp() is just a standalone static method that you would call directly when you want to exit the app in ways other than the hardware back button press. You would not use it inside of the BackAndroid.addEventListener.
The BackAndroid.addEventListener listens for the native hardware back button press and will ignore it if you return true, or execute the native back press (and exit the app) if you return false. This gives you the ability to navigate back through your navigation stack or tabs without exiting the app. Also gives you the ability to do any cleanup in your app before exiting the app.
Keep in mind that if you have multiple listeners setup you will have to be aware of the order of their subscriptions as noted by the docs:
"The event subscriptions are called in reverse order (i.e. last registered subscription first), and if one subscription returns true then subscriptions registered earlier will not be called."
seems BackAndroid.exitApp() won't kill the App's process ? 🤔
Yes
My code
componentWillMount() {
let self = this;
BackAndroid.addEventListener('hardwareBackPress', () => {
if(self.props.nav.index > 0) {
this.props.dispatch({ type:"Navigation/BACK" });
return true;
} else {
Alert.alert(
'Exit App',
'Exiting the application?',
[
{text: 'Cancel', onPress: () => console.log('Cancel Pressed'), style: 'cancel'},
{text: 'OK', onPress: () => DeviceEventManager.invokeDefaultBackPressHandler() },
],
{ cancelable: false }
)
return true;
}
});
}
BackAndroid.exitApp() won't kill the App's process
I just about to test DeviceEventManager.invokeDefaultBackPressHandler function.
Thanks dude . Seems so helpful . I am gonna try this .
On 16-Apr-2017 4:25 PM, "Tim Tawan" notifications@github.com wrote:
Yes
componentWillMount() {
let self = this;
BackAndroid.addEventListener('hardwareBackPress', () => {
if(self.props.nav.index > 0) {
this.props.dispatch({ type:"Navigation/BACK" });
return true;
} else {
Alert.alert(
'Exit App',
'Exiting the application?',
[
{text: 'Cancel', onPress: () => console.log('Cancel Pressed'), style:
'cancel'},
{text: 'OK', onPress: () => DeviceEventManager.
invokeDefaultBackPressHandler() },
],
{ cancelable: false }
)
return true;
}
});
}BackAndroid.exitApp() won't kill the App's process
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/facebook/react-native/issues/13483#issuecomment-294345654,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AKYl_xgfBxat6MQ5K1mNywgJh9WZ1Gksks5rwfONgaJpZM4M8LZ4
.
Same issue here,
I registered two scenes(SceneOne and SceneTwo) with react-navigation,both of them handle back button as exit
SceneOne component as root scene[SceneOne,SceneTwo].SceneTwo instead of SceneOneI have tried replace exitApp with DeviceEventManager.invokeDefaultBackPressHandler(),It doesn't work for me.
| software | version
| ---------------- | -------
| react-navigation | 1.0.0-beta.11
| react-native | 0.45.1
| node | 8.0.1
| yarn | 0.15.1
| OS|macOS 10.12.5
| android | 6.0.1
BackHandler.exitApp is not shutdown application but it call device to press back button. It seems in activity resume state RN resume all previous state.
I don't know why RN doesn't call finish method to current react Activity or kill process.
componentWillMount() {
let self = this;
BackAndroid.addEventListener('hardwareBackPress', () => {
if(self.props.nav.index > 0) {
this.props.dispatch({ type:"Navigation/BACK" });
return true;
} else {
Alert.alert(
'Exit App',
'Exiting the application?',
[
{text: 'Cancel', onPress: () => console.log('Cancel Pressed'), style: 'cancel'},
{text: 'OK', onPress: () => BackHandler.exitApp() },
],
{ cancelable: false }
)
return true;
}
});
}
@timtawan i made a little alteration in your code and BackHandler.exitApp() worked as desired .
If you're using wix/react-router-navigation make sure that you're not passing overrideBackPress: true to your scheme, otherwise the back press will be overriden.
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.
Most helpful comment
seems
BackAndroid.exitApp()won't kill the App's process ? 🤔