0

I have to know when my app comes back to the foreground in these situations:

  • Start app
  • Start Activity A
  • Home Pressed
  • App brought to foreground by user. (Activity A is shown)

  • Start app

  • Start Activity A
  • Start Activity B
  • Home Pressed
  • App brought to foreground by user. (Activity B is shown)

I cannot check this with a flag in onResume and onPause. Because this would trigger every time Activity B is loaded.

Vikalp Patel
  • 10,669
  • 6
  • 61
  • 96
Klaasvaak
  • 5,634
  • 15
  • 45
  • 68
  • Please check this question: http://stackoverflow.com/questions/8489993/check-android-application-is-in-foreground-or-not – Taras Mar 04 '13 at 15:29
  • If I understand that answer can check if the app is in foreground. Not if it came back to the foreground. – Klaasvaak Mar 04 '13 at 15:32
  • How about this ? http://stackoverflow.com/questions/6385620/notify-when-application-comes-to-foreground – Swayam Mar 04 '13 at 15:47
  • Also not. The problem in most answers on related questions is that they set something in onResume and remove it in onPause. This does not work because this will also trigger if an Activity is loaded from an other Activity. – Klaasvaak Mar 04 '13 at 15:50

1 Answers1

0

I'm not sure if this would work but try overriding the onUserLeaveHint of your Activity: https://developer.android.com/reference/android/app/Activity.html#onUserLeaveHint()

If this one is called, set a boolean field of your Activity to true. When the onResume is called, check for this boolean. If it is true, then your Activity is coming back into the foreground (don't forget to set this boolean back to false again).

BTW: I like your KlaasVaak handle :). Are your trying to make people sleepy :)

Streets Of Boston
  • 12,576
  • 2
  • 25
  • 28
  • It works in case the user presses the home button but the condition is also triggered when the user just switches to another app and then back. Only Klaasvaak can answer whether that would meet his requirements or not ;-) – Emanuel Moecklin Mar 04 '13 at 17:00
  • onUserLeaveHint is also called when an activity is started for result. For example the camera. So this would trigger the coming back to background. I do not see this as coming back from the background. – Klaasvaak Mar 07 '13 at 13:14