1

navigation flow map (Sorry, I cannot post image)

I am using React Native for Android Application and React Navigation. Now at Screen 0 (S0). Then did following navigations.

  1. this.props.navigation.navigate("S1")
  2. this.props.navigation.navigate("A1")
  3. this.props.navigation.navigate("A2")
  4. this.props.navigation.navigate("A3")

Now, once Actions in A3 is done, I have to navigate back to Screen 1 (S1). However, if I use this.props.navigation.navigate("S1"), what happened is that I can Press Back button (hardware or Header bar) and it go back to Screen A3.

When the back button is pressed on S1, I want to go back to S0. I don't want to set it manually as well because many other pages can come to S1.

Any suggestions or ideas how to make this work? I have tried popToTop() and goBack("A") but both are not working.

1
  • I cannot answer you in detail because I am on the jump to leave office. But I achieved the same handling by using two different navigators. If this is not working you could try to manually change the navigator stack. This stack should be able to be accessed through this.props.navigation - By the way please insert which navigator you are using through the fact that there are several's out there. Commented May 9, 2018 at 15:11

2 Answers 2

2

You can use NavigationActions.reset to get back to your first screen or NavigationActions.pop to "pop" back to the screen you want.

Sample code for nav reset:

    this.props.navigation.dispatch(NavigationActions.reset({
            index: 0,
            actions: [
                    NavigationActions.navigate({ routeName: 'S0' }),
                    NavigationActions.navigate({ routeName: 'S1' }),
                    NavigationActions.navigate({ routeName: 'A1' }),
                    NavigationActions.navigate({ routeName: 'A2' }),
                    NavigationActions.navigate({ routeName: 'A3' }),
            ],
    }));

Sample code for nav pop:

    this.props.navigation.dispatch(NavigationActions.pop({
            n: 3,
            immediate: true
    }));

Note: Remember to import { NavigationActions }

Sign up to request clarification or add additional context in comments.

Comments

0

I managed to make this work by just

this.props.navigation.pop(4). It will just back to 4th previous page and "Back" button still works as I intended. @Bryan 's suggestion works for me in one of the cases but I needed a much more general solution :D

Thank guys for helping! Really appreciate it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.