3

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.

Dusan
  • 3,284
  • 6
  • 25
  • 46

1 Answers1

1

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

Community
  • 1
  • 1
Charlie Collins
  • 8,806
  • 4
  • 32
  • 41
  • Does that mean, that i have to somehow save my spinner's states inside onPause and set the spinners back to their positions insede onResume? Is that the only option? My layout has lots of spinners and multichoice dialogs.. – Dusan Jan 06 '13 at 18:07
  • Yep, that's the way it's done. You save it in onSaveInstanceState and restore it in onRestoreInstanceState. It shouldn't be that bad though, even with many items. It's generally very simple state that is related to the "instance," such as what position your spinner is in. Just create a simple object for the state, and use it for all fields. Then parse it to save/restore state in those methods. – Charlie Collins Jan 07 '13 at 15:23
  • That's what i was avoiding.. The thing is... After about 9 spinners, i have a list, where each row can have 3 different states. And by clicking each row, a multichoice dialog pops up. All of this is reset when screen locks. So its not going to be easy to restore everything in onSaveInstanceState. But seems like, it's the only way to do it properly. Anyway, thanks for your help! – Dusan Jan 07 '13 at 15:39