S.M_Emamian
S.M_Emamian

Reputation: 17393

how to close first screen after open second screen

I'm using react-navigation.

I have a stackNavigation like this:

export const HomeStack = StackNavigator({
  Splash: {
    screen: Splash,
  },
  Home: {
    screen: Home,
  },
  Options: {
    screen: Options,
  },
},);

at the first time, Splash screen opened and I use this code to run Home screen:

Splash:

  this.props.navigation.navigate('Home');

when Home screen is opened I want to close Splash screen. but when I use back button on my android device, it shows Splash screen.

Upvotes: 0

Views: 391

Answers (2)

Naleen Dissanayake
Naleen Dissanayake

Reputation: 181

You can achieve that in simple way. use replace instead of navigate

this.props.navigation.replace('Home');

Upvotes: 1

XavierBrt
XavierBrt

Reputation: 1249

You need to reset the navigation index before to navigate to the 'Home' page, with the reset() function from StackActions. In this way, the page you are navigating to becomes the new root of the app. Here is the example from the doc :

import { StackActions, NavigationActions } from 'react-navigation';

const resetAction = StackActions.reset({
  index: 0,
  actions: [NavigationActions.navigate({ routeName: 'Home' })],
});
this.props.navigation.dispatch(resetAction);

See the doc : https://reactnavigation.org/docs/en/stack-actions.html#reset

Upvotes: 1

Related Questions