0

I'm developing with API level 8 on android 2.2

I read this about the life cycle of an android app : http://developer.android.com/reference/android/app/Activity.html but there is no comments about the hibernation state and how to handle it correctly...

when I switch off the device (and when my app is still running) the os calls the onDestroy method... and when I switch on the device my app start from the beginning, the os calls the onCreate method...

it could be better that the os calls only the onPause / onResume on hibernation, no ?

I look about Angry Birds on android and it is still living when the device switch on, they don't reload textures or anything else like one instant...

so how to do the same ? :)

solilab
  • 1
  • 4
  • Just speculation, but it's possible that your app is being destroyed and recreated due something other than CPU suspend. For example, if you do not handle configuration changes, and orientation change may result in a destroy/create cycle, and many devices will (inexplicably) change orientation on you every time the screen goes off/on. – Chris Stratton Apr 27 '12 at 17:57

2 Answers2

0

you would need a partial wake lock, so that your application would continue to run even when the screen is switched off. In a partial wake lock, the CPU is not put to sleep and only the screen is dimmed. I think this is what the Angry Birds game does. http://developer.android.com/reference/android/os/PowerManager.WakeLock.html

Akhil
  • 13,888
  • 7
  • 35
  • 39
  • Holding a wakelock just for faster resumption seems like bad practice, as it would be detrimental to battery life. If you really insist on doing that, at least time out and release it after a while. – Chris Stratton Apr 27 '12 at 17:55
  • yes, it would a bad idea to hold the lock indefinitely. One can also try to saving all the UI state and reproducing it so that the use doesnt feel that the app has restarted. – Akhil Apr 27 '12 at 17:59
  • yes, it looks like not a good idea to lock it on hibernation, I read the manifest xml file from the Angry Birds app and they don't use the permission 'WAKE_LOCK' so they seems to not use this method... – solilab May 01 '12 at 12:35
  • I also tried the 'getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)' method and no chance too... the onDestroy method is called from the os and the activity is killed – solilab May 01 '12 at 12:36
0

add in the manifest xml file :

  • android:launchMode="singleTask"
  • android:configChanges="keyboardHidden|orientation"

thx to Chris & to all

solilab
  • 1
  • 4