How to gracefully close an app that is in the background?
<p>I have an app which I want to self terminate if left idle for 120 seconds in the background. So for example, lets say my app is being used and I tap the HOME key then tap the Browser icon, my app will be pushed to the background while the Browser app is in the foreground. I have a Runnable timer going that after 120 seconds will call:</p>

<pre><code> Intent intent;
 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 myActivity.startActivity(intent);
</code></pre>

<p>which inside my myActivity class for the onCreate() method I call finish() to close that app (if a global flag is set to do so). </p>

<p>This works fine, except when I'm using the Browser app during the moment when my background app is closed, at which the Browser app is sent to the background and I just see my home screen page. I surmise this is because my app pops to the foreground for a few seconds before being closed, and that causes the Browser app to be pushed to the background.</p>

<p>I know there's other methods for killing my job (e.g., System.exit(0) ), however, when I try using that method I get "Forced Closed" error message if I try to run my app again afterwards. I also understand that using the System.exit() method isn't recommended as it doesn't clean up my app as does a call to finish().</p>

<p>Anyone have an idea to how I can gracefully close my app while in the background, without affecting the foreground running app?</p>

<p>Thanks.</p>
