I'm developing an app on 2.2.2, and need to take certain actions when the screen blanks, user presses 'home' etc.

Most of this is fine: when screen blanks, onPause() is called, then onResume() on an unblank, and if home is pressed it goes onPause(), onStop() etc.

However, that is all true for the main activity, it doesn't seem to apply as clearly for a secondary activity, started from the launch activity.

Screen blank / unblank works as expected, however on a 'home' key press only onPause() is called, not onStop() as well, and then restarting the app causes an onResume().

Ie, I cannot tell the difference between a screen blank and back to home screen from this secondary activity, and I need to, the reason being I need to carry out certain actions on a return to home, but not on a screen blank.

What could I do to find out?

有帮助吗?

解决方案 2

Actually the best way of doing this has proved to be using onUserLeaveHint(). This does get called from a subsidiary activity when Home is pressed - on API level 8 at least.

其他提示

Capturing the home button pressing event cannot be done just like that, and this is on purpose, so that you won't be able to block it.

If you do insist of capturing it, you will need to make your app a launcher and not a simple app.

As for the other events, you need to know that many of the methods will only tell you what has happened but not the trigger for them.

For this you will have to figure it out yourself.

For example, if you went to another activity, you can store it in a flag that says that you've went to another activity. If you need to know that you've returned from the second activity to the first one, starting the second activity should be done using startActivityForResult, and you will check the result later (and set it on the second activity).

If you want to avoid the activities switching, consider using fragments instead.

btw, you might also want to check out the method onWindowFocusChanged.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top