3

I am a developing game using andengine. When I press the power button, the onPause() method is not being called. How do I fix the issue?

    @Override
protected void onPause() {      
    //this method is not calling when press power button 
    super.onPause(); 
    this.mEngine.onPause() ;
}
Curtis
  • 101,612
  • 66
  • 270
  • 352
Bhoomika
  • 53
  • 2
  • 4

1 Answers1

2

Weird, haven't heard of it not being called. Let me research a bit.

Okay, pressing power should always call onPause(), there seem to be some kind of bug in your code. Could you add some log calls to follow every step. and print the DDMS log.

Other possible problems:

It seems some devices misbehave and call onPause() twice when pressing the power button.

In this case try using the powerManager to see if the screen is actually turned of.

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (pm.isScreenOn()) {
   //now do stuff
}

Maybe onPause() is called but onCreate() is called right after ?

When you turn off the device, the lock screen is displayed. This typically forces the display to a particular orientation. If the screen is not currently in that orientation, it will need to be changed, and the top activity's configuration appropriately changed to match.

You can add config changes like this:

android:configChanges="keyboardHidden|orientation" android:configChanges="keyboardHidden|screensize"

To make sure this doesn't happen.

Timmetje
  • 7,641
  • 18
  • 36
  • I'm not sure why it should. Would appreciate any reference to the documentation. I thought that sometimes on some devices lock button might be pressed by ancident and so it might be reasonable to keep activity for some seconds. – sandrstar Feb 22 '13 at 13:33
  • When the Power Button is pressed it will call the onPause() method of your application, and when you unlock the device it will call onResume(). This is how the Android Activity lifecycle is being managed by android O/S – Timmetje Feb 22 '13 at 13:37
  • 1
    If an activity in the foreground of the screen (at the top of the stack), it is active or running. If you press power, your home screen has takken focus of your activity, and turning your screen off. If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle http://stackoverflow.com/questions/6848518/detect-on-off-key-press-android – Timmetje Feb 22 '13 at 13:40
  • This is how it should behave unless phone settings can change power behavior making it do absolutely do nothing. But power button always makes your activity lose focus. making at **least** call onPause() because the activity will follow its state path. And is in the documentation I provided. – Timmetje Feb 22 '13 at 13:44
  • Yeah, it looks logical (and documentation provides explanation about loosing focus), however documentation still allow phone manufacturers not to start home by pressing power or introduce delays for its starting. Just wondering if it's implemented on any phone that way. – sandrstar Feb 22 '13 at 13:49
  • True but any other action except _nothing_ still loses focus on current activity. Thus calling `onPause()`. The only way to go around this is to make sure the power button does not work, and does nothing. Which i doubt any manufacturer will do. – Timmetje Feb 22 '13 at 13:50