react-native-navigation icon indicating copy to clipboard operation
react-native-navigation copied to clipboard

Override hardware back button

Open caktoy opened this issue 8 years ago • 9 comments

Issue Description

I want to exit my application when I pressed hardware back button, I was implemented BackHandler from react-native module, and I implement that in a modal. The event handler is worked only close active modal. How I can set to exit application when the event tirgerred?

Steps to Reproduce / Code Snippets / Screenshots

componentDidMount() { BackHandler.addEventListener('hardwareBackPress', this.handleBackPress); }

componentWillUnmount() { BackHandler.removeEventListener('hardwareBackPress', this.handleBackPress); }

handleBackPress() { Alert.alert('Exit', 'Are you sure?', [ { text: 'No', style: 'cancel' }, { text: 'Yes', onPress: () => BackHandler.exitApp() } ], { cancelable: false }); return true; }

Environment

  • React Native Navigation version: ^2.0.2381
  • React Native version: 0.55.4
  • Platform(s) (iOS, Android, or both?): both
  • Device info (Simulator/Device? OS version? Debug/Release?): Simulator Android 8

caktoy avatar Jul 01 '18 14:07 caktoy

Hi, Have you solved this ?

HRCJ7 avatar Jul 24 '18 05:07 HRCJ7

Not yet @HRCJ7

caktoy avatar Aug 02 '18 02:08 caktoy

Try this, to exit the app on exception.

	onHardwareBackPress = async () => {
		try {
			const { componentId } = this.props;
			await Navigation.pop(componentId);
			return true;
		} catch (e) {
			BackHandler.exitApp();
			return false;
		}
	};

Anarcho-X avatar Aug 31 '18 12:08 Anarcho-X

had the same issue with LightBox I change onBackhPressed() method.

@Override public void onBackPressed() {
        if (cancelable) {
           // hide();
        }
    }

in your case you should do the same for Modal class.

@Override
    public void onBackPressed() {
        if (!layout.onBackPressed()) {
           // super.onBackPressed();
        }
    }

kevinNejad avatar Sep 08 '18 12:09 kevinNejad

Any updates on how to override the back handling in a component?

muhammadsr avatar Oct 01 '18 19:10 muhammadsr

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you believe the issue is still relevant, please test on the latest Detox and report back. Thank you for your contributions.

stale[bot] avatar Nov 26 '18 13:11 stale[bot]

The issue has been closed for inactivity.

stale[bot] avatar Dec 03 '18 13:12 stale[bot]

any update on this?

cinder92 avatar Mar 13 '19 03:03 cinder92

any update ?

ashish-lonare avatar May 15 '19 06:05 ashish-lonare