3

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...

Community
  • 1
  • 1
CodingBird
  • 705
  • 2
  • 11
  • 22
  • Are you releasing the camera in `onPause()` – FoamyGuy Sep 24 '13 at 02:06
  • When the surface view freezes is there anything in the logcat? – FoamyGuy Sep 24 '13 at 02:53
  • @FoamyGuy nope. no warning, no error.. only my debugging lines. – CodingBird Sep 24 '13 at 02:56
  • If you are using harware acceleration try turning it off, and if not try turning it on. Also test some different devices if you can. This behavior may not be an issue on every device. – FoamyGuy Sep 24 '13 at 03:32
  • @FoamyGuy what do you mean by hardware acceleration? May I know how to turn it on/off? I've tried on another device and the same issue happened. – CodingBird Sep 24 '13 at 03:46
  • It's an option in the manifest file. Search "Android Hardware Acceleration" you should find some pages telling you how to enable/disable it. – FoamyGuy Sep 24 '13 at 13:16

1 Answers1

2

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)

NikkyD
  • 2,209
  • 1
  • 16
  • 31
  • that can be a possibility! never check if the surface just change only without being destroyed. But what I did in the end is to destroy and re-create the surface view :) – CodingBird Dec 05 '13 at 07:33
  • For anyone coming here nowadays, `isScreenOn()` method has been deprecated in favor of `isInteractive()` method, but the whole idea still works :) – carlosrafaelgn Jan 14 '22 at 12:34