Question

does anybody know the difference between pressing the power button and the home button? In my app, I've tried putting a debugging statement in every lifecycle of the activity and both result the same, that is, the activity will be onPause and will be onRestart then onResume when the activity is re-opened (if we press home button earlier) or when we turn on the device again by pressing the power button (if we press power button earlier).

Having this same behaviour/sequence, I expected my camera app to run the same in both cases. But it's not. I used surfaceview to preview to camera. If the press the home button and then re-open the app, it'll run normally.

BUT, if I press the power button and then press it again to turn it on again, the surfaceView freezes and thus the preview freezes too. But the camera is actually still working (if I press the camera button, itll still capture a photo).

So I'm really stuck in what's the difference between this two...

EDIT:

After testing and testing, I've found out the difference is that when I press home button, the app will be onPause() and then the surfaceView will be destroyed. On the other hand, if I press the power button, the surfaceView is not destroyed.

Still confused on what causing the two different behavior though...

Was it helpful?

Solution

I have discovered the same issue and i believe that when the PowerButton is pressed, the device changes from landscape to portrait to display the lock-screen. In my case i see the surfaceChanged instead of the expected destroyed, but the width and height show that the change went from landscape to portrait.

Edit

On my Galaxy S2 with 4.1.2 when i press the PowerButton the device only goes to "onPause" and the surface only changes (from landscape to portrait). When i hit the HomeButton, then it goes to "onStop" and the surface is destroyed without a change.

If you try the following in "onPause" you can determine whether the screen is still on or off

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn();

API lvl 7.

AFAIK it is safe to call "onStop" yourself in "onPause" as you cant cause the true lifecycle to change, you just execute the code that you wrote into the stop (the super method seems to do nothing, i checked the Activity source)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top