How to keep my app running on Android when I lock the screen?

user1539362 picture user1539362 · Jul 22, 2012 · Viewed 13.9k times · Source

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.

Answer

Axesh Ajmera picture Axesh Ajmera · Jul 22, 2012

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