I have developed an Android app which is working fine but while the app works if I plug the device into a car charger and start the vehicle the activity gets auto restarted and all the values in the current activity get lost. Is someone experienced in this and has any solution? Can DC current effect the device or activity?
Android is restarting your activity because of the configuration change. Try to use ViewModel to save activity state when the activity destroyed.
There are many reasons your activity can get destroyed, recreated/restarted, so you only need to prepare your app for any scenario that may come up, such that the activity instance state can be saved.
You can use any of the following depending on your scenario or preference:
Save data in a Bundle in the OnSaveInstanceState
callback method.
ViewModel (with LiveData).
Shared Preferences
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With