Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native Android, several apps instances running at the same time as result of coming from a background state without have killed the app before

Below is the process to reproduce the error.

Happening in my project v0.29, but i just tested init a new on v0.33 and it's behaving the same way.

When the app is running, press on the home button of the device/simulator and open the app by clicking on the desktop icon, what happens here is that the app restarts mounting the component again without have unmounted it before, resulting in several instances of the app running if you do this many times.

enter image description here

Then, if you press the device back button it will kill and so unmount the top instance letting you with the previous one until the last is unmounted and it will go to desktop.

enter image description here

After this if you exit the app as you did initially pressing on the home button and open it by clicking on the desktop icon, this won't restart the app mounting it again, behaving from this moment correctly. In the same way, if once the app first starts you exit it using the back button, afterwards the home button will behave right.

Also in the same manner, If you implement a package like react-native-activity-android to avoid kill the app when the back button is pressed, you will end up with multiple apps running.

enter image description here

enter image description here

I don't know about android, what is happening when the app is killed that after this if you send the app to the background using home button it works properly?

Any ideas how to solve this, so sending the app to the background and reopen it doesn't mount multiples apps?

In case i need to have the app running in the background for location/notification purposes, supposing i can't solve this issue but i can handle events listeners so they aren't executed multiple times. How bad is for performance/memory to have multiples apps mounted at the same time?

Thanks

like image 290
Norber S.M Avatar asked Sep 23 '16 09:09

Norber S.M


People also ask

How to run react native app on Android?

We can run the React Native app on Android platform by running the following code in the terminal. Before you can run your app on Android device, you need to enable USB Debugging inside the Developer Options. When USB Debugging is enabled, you can plug in your device and run the code snippet given above. The Native Android emulator is slow.

Why doesn't react native support background processing?

That’s because the only way which React Native itself supports background processing is Headless JS, and that headless thing is using Service, and thus it will allow you to write your logic in JavaScript rather than natively in Java\Kotlin.

Is it better to use service or app services in React Native?

Still, for React Native specifically, if you want something to start while using the app and continue even if the user left the app (like finish downloading a file, etc.), using Service is perhaps your best bet.

What is the best way to wake up a react app?

For React Native apps, if you want to wake up your app periodically for some tasks, the best way seems to be using some libraries like react-native-background-fetch, react-native-background-task, etc.


1 Answers

This bug is due to React Native creating more than one activity per application, which results in multiple root components running at the same time.

A solution that seems to work for most people is to set the launchMode to singleInstance in AndroidManifest.xml, so something like this:

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

More information there:

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

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

like image 106
laurent Avatar answered Sep 27 '22 23:09

laurent