Browse Source

Handle back press in JS

Guy Carmeli 8 years ago
parent
commit
aef799a9f9

+ 1
- 0
example-redux/src/app.js View File

@@ -121,6 +121,7 @@ export default class App {
121 121
               icon: require('../img/one.png'),
122 122
               selectedIcon: require('../img/one_selected.png'),
123 123
               title: 'Screen One',
124
+              overrideBackPress: true,
124 125
               navigatorStyle: {}
125 126
             },
126 127
             {

+ 1
- 0
example-redux/src/screens/FirstTabScreen.js View File

@@ -181,6 +181,7 @@ class FirstTabScreen extends Component {
181 181
     this.props.navigator.push({
182 182
       title: "More",
183 183
       screen: "example.PushedScreen",
184
+      overrideBackPress: true,
184 185
       passProps: {
185 186
         str: 'This is a prop passed in \'navigator.push()\'!',
186 187
         obj: {

+ 17
- 0
example-redux/src/screens/PushedScreen.js View File

@@ -62,9 +62,26 @@ class PushedScreen extends Component {
62 62
       case 'accept':
63 63
         Alert.alert('NavBar', 'Accept button pressed');
64 64
         break;
65
+
66
+      case 'backPress':
67
+        this.handleBackPress();
68
+        break;
69
+
70
+      default:
71
+        console.log('PushedScreen', 'Unknown event ' + event.id);
65 72
     }
66 73
   }
67 74
 
75
+  handleBackPress() {
76
+    Alert.alert(
77
+      'Back button press!',
78
+      'Handling back press in JS',
79
+      [
80
+        {text: 'Pop', onPress: () => this.props.navigator.pop()}
81
+      ]
82
+    )
83
+  }
84
+
68 85
   render() {
69 86
     return (
70 87
       <ScrollView style={{flex: 1, padding: 20, backgroundColor: this.bgColor}}>