4

I'm developing an Android app. How can I make the app not to be killed when I lock the screen, so that when I unlock the phone, my app is there ready for use?

Just like games. when I unlock the screen the game is still live.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
user1539362
  • 53
  • 1
  • 1
  • 3
  • 1
    what operating system, platform? – Jure C. Jul 21 '12 at 23:30
  • sorry, i am new to this :) android ics 4.0.4 – user1539362 Jul 21 '12 at 23:47
  • I didn't have to anything special to make this work on my app. What behavior are you seeing? – levis501 Jul 22 '12 at 00:55
  • I have my app running, when I push home button and re-open my app it is still running normally, but when I turn off the screen, and then turn it on again and unlock the phone, my app restarts itself, so I lose what I was currently doing – user1539362 Jul 22 '12 at 01:12
  • When you press the lock screen basically wt happens is that game goes to a pause state of the activity lifecycle.You can verify this by – Axesh Ajmera Jul 22 '12 at 01:50

1 Answers1

2

When you press the lock screen basically wt happens is that game goes to a pause state of the activity lifecycle.You can verify this by by adding some logs in the onPause method.

In addition when an activity is in pause state and if it hogs a lot of memory android system has right to kill your app. Also you add some logs in the onResume method of Activity lifecycle.

I would highly recommend you to refer the link below to understand android activity lifecycle.Also note general pattern of game is to save the state of game in the onPause method and then retrieve the the saved state of your game in your onResume method.

Also make sure you release objects in the onPause method since they are hogging memory system seems to be killing your app .For Eg:- if you are using something like bitmap object oyu can release that object.

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

Axesh Ajmera
  • 383
  • 2
  • 8