How to close a screen in React Native from another

0

I need to know how I close the screens I've opened up. Type: I'm going to A - B - C - D ... when I get to D, I want to go back to A. But if I push the back button on both Android and iOS, it will exit the application, not back D - C - B - A to exit. I'm using Navigation to move between screens.

    
asked by anonymous 22.06.2018 / 03:13

1 answer

0

Oops, is everything good?

If you are using the React-Navigation library, you can manage the Android Back using a < in> BackHandler .

Example:

import React from 'react';
import {..., BackHandler, ... } from 'react-native';
export default class Main extends React.Component {
// ...
  exemploDeHandler(){
  //Faça Alguma coisa;
  if (!this.onMainScreen()) {
    this.goBack();
    return true;
  }
  return false;

  // Retornar true no Handler,
  // Segundo a documentação(Link logo Abaixo do Código)
  // evita a execução do comportamento default
  }

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

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

Documentation: BackHandler

    
11.09.2018 / 17:15