Check when my app is in background when back-button and home-button
<p>Hi guys I'm using this method to check if my app is in background or not:</p>

<pre><code>public static boolean isApplicationSentToBackground(final Context context) {
    ActivityManager am = (ActivityManager) context
            .getSystemService(Context.ACTIVITY_SERVICE);
    List&lt;RunningTaskInfo&gt; tasks = am.getRunningTasks(1);
    for( RunningTaskInfo rti: tasks ) {
        ComponentName cn = rti.topActivity;
        LogHelper.i(TAG, "Component Name: " + cn.getPackageName() );
    }



    if (!tasks.isEmpty()) {
        ComponentName topActivity = tasks.get(0).topActivity;
        if (!topActivity.getPackageName().equals(context.getPackageName())) {
            return true;
        }
    }

    return false;
}
</code></pre>

<p>I'm calling this method in my Activity onPause, onStop, onDestroy
These is my results: </p>

<p>when I'm in my activity and I click the android-home-button it follow this flow:</p>

<p>onPause -> is in background? true
onStop -> is in background? true</p>

<p>and it's all perfect.
The problem is when I click the android-back-button that from th activity goes back to the android home. This is the flow:</p>

<p>onPause -> is in background? false
onStop -> is in background? false
onDestroy -> is in background? false</p>

<p>If you check the code I have a Log cat stamp and it always (in the back-button case) stamp this:</p>

<p><strong>03-29 00:46:39.324: I/DFFramework(15344): MusicUtils - Component Name: my-activity-name</strong></p>

<p>This means that my activity is not in the background.
What can I do?</p>

<p>UPDATE:</p>

<p>I'm adding more info about my scenario. I'm developing a Music Player so I've to catch this kind of event in order to start my foreground audio service if the app is closed but the music is still playing (otherwise the music will be paused).
So I've to catch when the app is in background (like when you click the android-home-button) or when you are closing the application (for example when you click android-back-button).</p>
