Reputation: 329
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:
Intent intent;
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
myActivity.startActivity(intent);
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).
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.
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().
Anyone have an idea to how I can gracefully close my app while in the background, without affecting the foreground running app?
Thanks.
Upvotes: 0
Views: 1471
Reputation: 333
I believe you can call FLAG_ACTIVITY_CLEAR_TOP as.an intent flag and all but the activity that's started from that intent will be destroyed, and then you can call finish() on that one as well and be 100% exited.
Upvotes: 0
Reputation: 2774
Your process keeps running in the background, it is not “stopped” or “suspended” in any way (only the UI is supposed to be inactive). So you can post a timer etc which will call finish on your activity after a suitable interval.
Upvotes: 0
Reputation: 1084
You can use the onPause()
method to perform tasks when your program loses focus.
http://developer.android.com/reference/android/app/Activity.html#onPause()
You can then use the Timer class to call finish
after a set period of time.
Upvotes: 0
Reputation: 6131
You can check in your onResume()
method if the Timer has expired. When yes you simple set your Data to emtpy and call finish()
Upvotes: 2