Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android activity restarting

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?

like image 757
Ashana.Jackol Avatar asked Apr 01 '19 05:04

Ashana.Jackol


2 Answers

Android is restarting your activity because of the configuration change. Try to use ViewModel to save activity state when the activity destroyed.

like image 162
Amirhosein Heydari Avatar answered Oct 20 '22 12:10

Amirhosein Heydari


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:

  1. Save data in a Bundle in the OnSaveInstanceState callback method.

  2. ViewModel (with LiveData).

  3. Shared Preferences

like image 1
lekanbaruwa Avatar answered Oct 20 '22 11:10

lekanbaruwa