React Native Android, several instances of applications running at the same time that they exit the background state did not kill the application before

The following is the error reproducing process.

It happens in my v0.29 project, but I just tested init on v0.33 and it behaves the same.

When the application is running, click on the home button of the device / simulator and open the application by clicking on the desktop icon, what happens here when the application restarts installing the component again without unmounting it earlier, as a result of which several instances of the application work if you do it is many times.

enter image description here

, , , , , .

enter image description here

, , "" , , , , , . , , "", "" .

, , action-native-activity-android, , "", .

enter image description here

enter image description here

, , , , , ?

, , ?

, , /, , , , . / ?

+4
3

, - , .

- , React Native , .

, , , , - launchMode singleInstance AndroidManifest.xml, - :

<manifest>
    ...
    <application>
        ...
        <activity ... android:launchMode="singleInstance">
        </activity>
    </application>
</manifest>

:

https://github.com/facebook/react-native/issues/10266

https://github.com/facebook/react-native/issues/7079

+3

, BackAndroid false . , .

android:launchMode="singleTask" AndroidManifest.xml . , react-native-activity-android, ActivityAndroid.moveTaskToBack(), , return false. :

handleBackPress() {
    if (this.navigator) {
        if (this.navigator.getCurrentRoutes().length > 1) {
            this.navigator.pop();
        } else {
            ActivityAndroid.moveTaskToBack();
        }
    }
    return true;
};

. React Native 0.29.0 . , , .

0

In fact, you can implement your own module to fix this problem. It is pretty simple. Add this piece of code to one of your own modules.

@ReactMethod
public void moveTaskToBack(Callback cb) {
    Activity activity = getCurrentActivity();
    boolean wasMoved = activity.moveTaskToBack(true);
    if (cb != null) {
        cb.invoke(wasMoved);
    }
}

You can then handle the onBackPress event, as Ryan suggested. Always return true and call moveTaskToBack () if necessary.

0
source

Source: https://habr.com/ru/post/1655591/


All Articles