0

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?

nmw01223
  • 1,611
  • 3
  • 23
  • 40
  • Instead of trying to listen for the Activity callback methods to figure out the state of the screen, you should just use this intent filter: https://developer.android.com/reference/android/content/Intent.html#ACTION_SCREEN_OFF – hwrdprkns Mar 31 '13 at 08:38
  • And here is an example: Listening for ACTION_SCREEN_OFF on Android - http://stackoverflow.com/questions/11346958/listening-for-action-screen-off-on-android – Tigger Mar 31 '13 at 08:41
  • Thanks, useful. I'm thinking ACTION_USER_BACKGROUND / FOREGROUND might be what I need. I'll experiment. – nmw01223 Mar 31 '13 at 09:33
  • what exactly do you mean by screen blank, activity transition? screen may go blank even when device is locked – Calvin Mar 31 '13 at 09:54
  • Basically I need to differentiate between the screen timing out / going blank / locking, and an explicit 'home' press. – nmw01223 Mar 31 '13 at 11:06

2 Answers2

0

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.

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • In general, each time you find an answer to fit your needs and you think it has answered your question, you tick it (click the "V" button) as "answered". – android developer Mar 31 '13 at 11:22
0

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.

nmw01223
  • 1,611
  • 3
  • 23
  • 40