Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent recreating activity after unlocking screen

i have some spinners in my activity. The screen is set to lock after 2 minutes. After I unlock the screen, all of my spinners are reset. I think, that the onCreate method is called after unlocking. Is there some way, to set the activity not to recreate after unlocking the screen? The same happens when rotating the screen, but that's not the issue, i can set the layout to be portrait only.

Or maybe is there some setting, like for rotation, to prevent calling the onCreate method again?:

android:configChanges="orientation"

thanks.

like image 862
Dusan Avatar asked Mar 07 '26 09:03

Dusan


1 Answers

http://developer.android.com/reference/android/app/Activity.html

"An activity can frequently go between the resumed and paused states -- for example when the device goes to sleep"

http://developer.android.com/training/basics/activity-lifecycle/pausing.html

You need to manage state using the Activity lifecycle methods, rather than trying to "set the activity not to recreate." Activitys are created/destroyed/recreated all the time, by design (and for the record locking to one orientation is also sort of a band-aid, dealing with state correctly will mean you don't have to do that either).

This question may help, the answer has an example of dealing with instance state: Saving Android Activity state using Save Instance State

like image 142
Charlie Collins Avatar answered Mar 09 '26 23:03

Charlie Collins



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!