1

I try to make Notification which must work only when Application UI isn't visible. I tried to store preference which was written in onStart() and onStop() of my Activity. But sometimes, it's not working because another application became visible without MyActivity.onStop() being called.

What other method I can use for a Service to determine, if MyApplication is visible now? Or, maybe MyActivity?

Nate
  • 31,017
  • 13
  • 83
  • 207
Konstantin.Efimenko
  • 1,242
  • 1
  • 14
  • 38

1 Answers1

1

If you already have code to keep track of the state of your app's UI, you can probably get it to work simply by putting the code in onPause() and onResume(), instead of onStart() and onStop().

It is possible for the UI not to be visible, or partially hidden, even before onStop() gets called ... as you found out.

Take a look at the Android Activity lifecycle diagram here:

http://developer.android.com/images/activity_lifecycle.png

and note the description:

The foreground lifetime of an activity happens between a call to onResume() until a corresponding call to onPause(). During this time the activity is in front of all other activities and interacting with the user. An activity can frequently go between the resumed and paused states -- for example when the device goes to sleep, when an activity result is delivered, when a new intent is delivered -- so the code in these methods should be fairly lightweight.

Read more about this in another question here.

Community
  • 1
  • 1
Nate
  • 31,017
  • 13
  • 83
  • 207