'Physical Back Button Behaviour with react navigation

I have tab navigation and I need to create a single back button for all of the screens. For now, I am creating a back button in all of the screens and calling navigation.goBack() from all of them when back pressed because the navigation object is available inside screens only.

Is there a way where I can create a separate back button and then call navigation.goBack() to whichever the current screen is in the navigator?

Also, I have nested navigation also like stack navigator inside tab navigator, so I need to call the deepest navigator's go back function.

for example,

TabNavigator
  tab1: screen1
  tab2: stackNavigator
          stack1: screen2
          stack2: screen3

and when I press the back button on screen 3, I should navigate to screen 2, not tab 1. Like the physical back-button behaves in react-navigation in android OS.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source