Roy Hinkley
Roy Hinkley

Reputation: 10641

Different behavior depending upon how screen is turned off

I am finding a weird situation depending upon how the screen is turned off. If Android turns off the screen or the power button is pressed to turn off the screen, my app as expected is paused. When the power button is pressed to turn the screen back on, the app resumes no issues. This is true for a few moments or when tens of minutes pass.

Here's where it deviates. If my app turns the screen off, my app pauses as expected, but my app is also killed immediately.

When the screen is turned back on with the power button, the app is no longer running

I have set up logging that confirms this, but why the difference in behavior?

Upvotes: 0

Views: 216

Answers (1)

Martin Cazares
Martin Cazares

Reputation: 13705

The behaviour you are seeing is actually expected, there's a priority for each of the processes running in an Android Device, once your application goes to pause it might become elegible by the OS to get rid of it if another process with higher priority requires memory, as per google's documentation:

The Android system tries to maintain an application process for as long as possible, but eventually needs to remove old processes to reclaim memory for new or more important processes. To determine which processes to keep and which to kill, the system places each process into an "importance hierarchy" based on the components running in the process and the state of those components. Processes with the lowest importance are eliminated first, then those with the next lowest importance, and so on, as necessary to recover system resources.

As explained before "based on the components running in the process" means that your application might not be highly ranked in the OS, since you might have not Service running, etc, hence it is being killed eventually...

You can look at this document: http://developer.android.com/guide/components/processes-and-threads.html, it explains the process life cycle and should give you a better understanding on what to do to deal with it...

Also in the battle to make battery last longer in Android devices there's a: Suspend/Resume state on the device. Suspend and resume happen in a three-step sequence and thus are best discussed together. Basically, when the device is suspended with the power button or the screen saver timeout expires, the device suspends. If the power button is pressed again, it resumes to the lock-screen. At this point, if the user unlocks the device, the application is resumed. If the user waits several seconds without unlocking the lock screen, the device will suspend again.

Since this is explicitly a low-power state, the application should have stopped all rendering and sound, and likely any background processing that isn’t 100% required to keep the app alive.

Regards!

Upvotes: 1

Related Questions