react-native-back-android

A Higher-order Component for handling back button press on React Native Android.

Usage

(Take Navigator as an example. Can be used in any scenario.)

import React, { Component } from 'react'
import { AppRegistry, Button, Text, View, Alert } from 'react-native'
import { StackNavigator } from 'react-navigation'
import backAndroid, {
  hardwareBackPress,
  exitApp
} from 'react-native-back-android'

class HomeScreen extends React.Component {
  static navigationOptions = {
    title: 'Welcome'
  }
  // reserved function for handling hardware back press
  handleHardwareBackPress = () => {
    Alert.alert(
      'Quiting',
      'Want to quit?',
      [
        {
          text: 'Cancel',
          onPress: () => console.log('Cancel Pressed'),
          style: 'cancel'
        },
        { text: 'OK', onPress: () => exitApp() }
      ],
      { cancelable: false }
    );
    // return true to stop bubbling
    return true
  };
  render () {
    const { navigate } = this.props.navigation
    return (
      <View>
        <Text>Hello, Chat App!</Text>
        <Button onPress={() => navigate('Chat')} title='Chat with Lucy' />
      </View>
    )
  }
}

class ChatScreen extends React.Component {
  static navigationOptions = {

GitHub