Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React native android moveTaskToBack?

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.

like image 354
Pewh Gosh Avatar asked Jul 14 '16 07:07

Pewh Gosh


2 Answers

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

like image 200
martial Avatar answered Nov 07 '22 00:11

martial


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

like image 29
C-lio Garcia Avatar answered Nov 06 '22 23:11

C-lio Garcia