问题
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