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.