Android: How to detect if current stack of activities (task) moves to background?
<p>The official documentation describes tasks as follows:</p>

<blockquote>
  <p>*All the activities in a task move together as a unit. The entire task (the entire activity stack) can be brought to the foreground or sent to the background. Suppose, for instance, that the current task has four activities in its stack — three under the current activity. The user presses the HOME key, goes to the application launcher, and selects a new application (actually, a new task). The current task goes into the background and the root activity for the new task is displayed. Then, after a short period, the user goes back to the home screen and again selects the previous application (the previous task). That task, with all four activities in the stack, comes forward. </p>
</blockquote>

<p>Is there a way to programmatically detect when the task of the current Activity moves into and out of the background? I would like to know when the user has switched switched to another application, vs. when the user navigated to another Activity in the current app.</p>
