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?
Use the goBack() Method to Go Back One Screen in React Native. The goBack() method is one of the most important methods in the react-navigation library. It allows you to go back to one of the previous screens in your navigation stack.
If there are more than 1 screen on stack, device back button will show previous screen. 2. If there is only 1 screen on stack, device back button will exit app. Important: Don't forget to bind method in constructor and to remove listener in componentWillUnmount.
In this post I will discuss how you can implement "exiting your app when back button is pressed twice". So if your user is using your app and accidently presses the back button, the app will exit.
To handle the Android Back Button Press in the React Native we have to register the hardwareBackPress event listener with a callback function, which will be called after pressing the Back Button.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With