Selaa lähdekoodia

Handle back press in JS

Guy Carmeli 8 vuotta sitten
vanhempi
commit
aef799a9f9

+ 1
- 0
example-redux/src/app.js Näytä tiedosto

121
               icon: require('../img/one.png'),
121
               icon: require('../img/one.png'),
122
               selectedIcon: require('../img/one_selected.png'),
122
               selectedIcon: require('../img/one_selected.png'),
123
               title: 'Screen One',
123
               title: 'Screen One',
124
+              overrideBackPress: true,
124
               navigatorStyle: {}
125
               navigatorStyle: {}
125
             },
126
             },
126
             {
127
             {

+ 1
- 0
example-redux/src/screens/FirstTabScreen.js Näytä tiedosto

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

+ 17
- 0
example-redux/src/screens/PushedScreen.js Näytä tiedosto

62
       case 'accept':
62
       case 'accept':
63
         Alert.alert('NavBar', 'Accept button pressed');
63
         Alert.alert('NavBar', 'Accept button pressed');
64
         break;
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
   render() {
85
   render() {
69
     return (
86
     return (
70
       <ScrollView style={{flex: 1, padding: 20, backgroundColor: this.bgColor}}>
87
       <ScrollView style={{flex: 1, padding: 20, backgroundColor: this.bgColor}}>