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?
-
Is it restarting continuously? – Harish Jose Apr 01 '19 at 05:49
-
1Could there be a "BroadcastReceiver"? – Hasan Kucuk Apr 01 '19 at 05:58
-
See if this is the same issue as mentioned in the link here. https://stackoverflow.com/a/8453842/6169668 – ASN Apr 01 '19 at 06:03
-
1@HarishJose no not continuously just restart the activity once and thats it, – Ashana.Jackol Apr 01 '19 at 06:48
-
There is no any defined broad cast receiver for this particular scenario. – Ashana.Jackol Apr 01 '19 at 06:50
3 Answers
Android is restarting your activity because of the configuration change. Try to use ViewModel to save activity state when the activity destroyed.

- 4,266
- 4
- 22
- 35
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

- 167
- 1
- 4
Any app open on your device will restart when you plugged usb on your device.
When you're using fragment try to use onSaveInstanceState, try this link (Once for all, how to correctly save instance state of Fragments in back stack?)
When you're using activity try to use variable to store values of the views.

- 1
- 1