Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I Set back to last activity in react-native for android?

Tags:

react-native

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?

like image 621
jianfeng Avatar asked Oct 01 '15 06:10

jianfeng


People also ask

How do you go back to previous screen in React Native?

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.

How do you handle back navigation in React Native?

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.

How do I exit app React Native?

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.

How do you handle back press in React Native Android?

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.


1 Answers

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.

like image 90
Stefan Klasen Avatar answered Sep 27 '22 23:09

Stefan Klasen