Navigation — Moving Between Screens
Lesson opener
In one line: Why mobile navigation is a stack, not URLs.
Why it matters: This shows up at work when you need to why mobile navigation is a stack, not URLs.
Before this lesson: android2
Before you start
| Resource | Link |
|---|---|
| Previous | React Native fundamentals |
| Next | State & persistence |
| Prerequisites | android2 Hello World running in Expo Go |
| Time | ~55 minutes |
What you will learn
- Why mobile navigation is a stack, not URLs
- Installing React Navigation in Expo
- A complete 3-screen app: Home → Detail → Settings
- Android back button behaviour and why it matters for reviews
Section 1 — URLs vs stack (the concept web devs get wrong)
Plain English:
In a website, navigation is URLs. /home → home page. /about → about page. The browser handles history, back button, and deep linking.
In React Native, there are no URLs. Navigation is a stack of screens in memory. You push screens onto the stack and pop them off. The back button pops the top screen.
Stack after opening app: [ Home ]
Tap "View details": [ Home, Detail ]
Tap "Settings": [ Home, Detail, Settings ]
Press back: [ Home, Detail ]
Press back again: [ Home ]Section 2 — Install React Navigation
In your project folder:
npm install @react-navigation/native @react-navigation/native-stack
npx expo install react-native-screens react-native-safe-area-contextSection 3 — Three-screen app
File structure
MyFirstApp/
App.js
screens/
HomeScreen.js
DetailScreen.js
SettingsScreen.jsApp.js
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import HomeScreen from './screens/HomeScreen';
import DetailScreen from './screens/DetailScreen';
import SettingsScreen from './screens/SettingsScreen';
const Stack = createNativeStackNavigator();
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="Home">
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Detail" component={DetailScreen} />
<Stack.Screen name="Settings" component={SettingsScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}Breaking it down:
| Line | Plain English |
|---|---|
NavigationContainer | Wraps the app — required root for navigation |
createNativeStackNavigator() | Native stack transitions (slide on Android) |
Stack.Screen name="Home" | Registers a route name (not a URL path) |
component={HomeScreen} | Which React component to render |
screens/HomeScreen.js
import { View, Text, Button, StyleSheet } from 'react-native';
export default function HomeScreen({ navigation }) {
return (
<View style={styles.container}>
<Text style={styles.title}>Home</Text>
<Button
title="View details"
onPress={() => navigation.navigate('Detail')}
/>
<Button
title="Settings"
onPress={() => navigation.navigate('Settings')}
/>
</View>
);
}
const styles = StyleSheet.create({
container: { flex: 1, padding: 20, justifyContent: 'center' },
title: { fontSize: 24, fontWeight: 'bold', marginBottom: 20 },
});Breaking it down:
navigation— injected prop from React Navigationnavigation.navigate('Detail')— push Detail onto the stack
screens/DetailScreen.js
import { View, Text, StyleSheet } from 'react-native';
export default function DetailScreen({ route }) {
const itemId = route.params?.itemId ?? 'none';
return (
<View style={styles.container}>
<Text style={styles.title}>Detail Screen</Text>
<Text>Item id: {itemId}</Text>
</View>
);
}
const styles = StyleSheet.create({
container: { flex: 1, padding: 20 },
title: { fontSize: 24, fontWeight: 'bold' },
});Pass params from Home: navigation.navigate('Detail', { itemId: 42 })
screens/SettingsScreen.js
import { View, Text, StyleSheet } from 'react-native';
export default function SettingsScreen() {
return (
<View style={styles.container}>
<Text style={styles.title}>Settings</Text>
</View>
);
}
const styles = StyleSheet.create({
container: { flex: 1, padding: 20 },
title: { fontSize: 24, fontWeight: 'bold' },
});Section 4 — Android back button
Plain English:
Android has a hardware or gesture back button. React Navigation handles this automatically when you use NavigationContainer + stack navigator — back pops the stack.
If you build custom navigation without the library, you must handle it yourself with BackHandler — and every app that ignores this gets negative reviews.
Never block the back button without telling the user (e.g. "Discard unsaved changes?" dialog).
Practice
Do this now: Add a fourth screen. Navigate Home → Detail → Settings → back → back → Home. Confirm the Android back gesture matches the stack.
Checklist
- [ ] Can you explain stack navigation vs URLs?
- [ ] Can you
navigate()and passroute.params? - [ ] Does the system back button pop screens correctly?
What's next
State management & AsyncStorage — data that survives app restarts.