Reputation: 2590
How Can I Set back to last activity in react-native for android?
My thinking is:
1 set _navigate in index.android.js
// 设置回退的堆栈
var _navigator;
BackAndroid.addEventListener('hardwareBackPress', function() {
if (_navigator && _navigator.getCurrentRoutes().length > 1) {
_navigator.pop();
return true;
}
return false;
});
2 pass navigator in RouteMapping:
RouteMapper: function(route, navigationOperations, onComponentRef) {
_navigator = navigationOperations;
if (route.name === 'detail') {
// 问题详情页
return (
<DetailScreen
navigator={navigationOperations}
question={route.question} />
);
} else if (route.name == 'front') {
// 首页
return(
<FrontScreen
navigator={navigationOperations}
/>
);
}
},
3 set push in list view
gotoDetail: function(question: Object) {
this.props.navigator.push({
id: question.question_id,
name: 'detail',
question: question
})
But It not work. When I click back button in Android, it jump out the app?
How Can I do that?
Or Anyone can give some example?
Upvotes: 6
Views: 3605
Reputation: 141
You need to add the following code to <your project>/android/app/src/main/java/com/<your project>/MainActivity.java:
@Override
public void onBackPressed() {
if (mReactInstanceManager != null) {
mReactInstanceManager.onBackPressed();
} else {
super.onBackPressed();
}
}
Projects created with React Native 0.12 will behave correctly.
For more infos see this issue on Github https://github.com/facebook/react-native/issues/3223.
Upvotes: 3