logicalicy logicalicy - 1 year ago 106
Android Question

[React Native / Android]: How to correctly handle MainActivity getting destroyed on pressing back button?

My application is written with React Native with Redux. It appears as though my

is destroyed on back button press. When the app is re-opened from the Task Manager or re-launched from the app menu, the app has lost its previous state, which I would like to avoid or work around.

I have read solutions around overriding the
method with
, etc. but I'm not convinced it's the right solution.

Is there a clean / correct way to make sure that my app is restarted with the most recent application state? Perhaps this involves persisting the app state somewhere and restoring it when the application comes into the foreground.

Answer Source

The easiest way around this was to set the launchMode of MainActivity to singleTask in AndroidManifest.xml. Read more about launch modes in the Android docs.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download