React native android moveTaskToBack?

邮差的信 提交于 2021-01-27 04:10:40

问题


Is there any library which has ability like moveTaskToBack in React Native?

Previously I use https://github.com/jaysoo/react-native-activity-android and it has moveTaskToBack. But unfortunately this repo is not active anymore, and since React Native 0.29, it has some internal change which make that library didn't work.


回答1:


I'm not sure it's what you need, but if you put this in your MainActivity.java class:

@Override
    public void invokeDefaultOnBackPressed() {
        // do not call super.invokeDefaultOnBackPressed() as it will close the app.  Instead lets just put it in the background.
        moveTaskToBack(true);
    }

Then when user press the android back button on the "root" page, the app will go in background instead of being closed.

Source: background a react-native android app using back button




回答2:


Use react-native-navigation, then navigate to your projects node modules folder and locate the react-native-navigation folder and inside it, follow the path to the NavigationActivity.java:

/android/app/src/main/java/com/reactnativenavigation/controllers/NavigationActivity.java.

In the NavigationActivity.java file, you will see the following method at around line 196:

@Override
public void invokeDefaultOnBackPressed() {
    if (layout != null && !layout.onBackPressed()) {
        super.onBackPressed();
    }
}

Comment out the line: super.onBackPressed() and add the line: this.moveTaskToBack(true);

and thats all! Happy coding



来源:https://stackoverflow.com/questions/38368201/react-native-android-movetasktoback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!