if I hit the Powerbutton in my app while the app is currently running the onPause
in not called. First question: Is this usual for Android?
I tried to call theonPause
by setting an onKeyListener
. Can I set this onKeyListener
for all elements in the activity? E.g. I set it on my realativeLayout
.
rLayout.setOnKeyListener(new View.OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_POWER))
{
onPause();
}
return true;
}
});
But this also didn't work. So now the main question: Do you have any possibility to call onPause
if you hit the Powerbutton while the app is currently running? (Don't need to be an onKeyListener
)
As your activity enters the paused state, the system calls the onPause() method
http://developer.android.com/training/basics/activity-lifecycle/pausing.html
It's important to note that the app itself NEVER calls onPause itself because that would likely mess up the Activity's life cycle (which is managed by Android not the app). If the user presses the power button, the Activity will be paused by Android automatically, there's no need to do anything in your app. If you think the onPause isn't called, please post the onPause code.