Skip to content

Instantly share code, notes, and snippets.

@omeileo
Last active July 21, 2019 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omeileo/f05a068557e9f0a2d8a24ecccd2f3177 to your computer and use it in GitHub Desktop.
Save omeileo/f05a068557e9f0a2d8a24ecccd2f3177 to your computer and use it in GitHub Desktop.
Closing React Native Android App with Back Button (Using react-native-router-flux)
import { BackHandler } from 'react-native'
import { Actions, Router } from 'react-native-router-flux'
class App extends Component {
componentDidMount () {
this.backHandler = BackHandler.addEventListener('hardwareBackPress', this.handleBackPress)
}
componentWillUnmount() {
this.backHandler.remove()
}
handleBackPress = () => {
switch (Actions.currentScene) {
case 'home':
BackHandler.exitApp()
break
default: Actions.pop()
}
return true
}
// Define scenes for routing
const scenes = Actions.create(
<Scene key='root' navTransparent>
<Scene
initial={true}
key='home'
component={Home}
type='replace'
hideNavBar
/>
...
<Scene
key='signUp'
component={SignUp}
type='push'
renderBackButton={getBackButton}
/>
<Scene
key='signIn'
component={SignIn}
type='push'
renderBackButton={getBackButton}
/>
</Scene>
)
render () {
return <Router scenes={scenes} />
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment