tags:

views:

3001

answers:

6

I want to close my application, so that it no longer runs in the background.

How to do that? Is this good practice on Android platform?

If I rely on the "back" button, it closes the app, but it stays in background. There is even application called "TaskKiller" just to kill those apps in the background.

A: 

This is how Windows Mobile has worked for... well... ever! Here's what Microsoft have to say on the matter:

http://blogs.msdn.com/windowsmobile/archive/2006/10/05/The-Emperor-Has-No-Close.aspx (is it sad that I remembered the title of the blog post all the way from 2006? I found the article on Google by searching "the emperor has no close" lol)

In short:

If the system needs more memory while the app is in the background, it’ll close the app. But, if the system doesn’t need more memory, the app will stay in RAM and be ready to come back quickly the next time the user needs it.

Many comments in this question at O'Reilly suggest that Android behaves in much the same way, closing applications that haven't been used for a while only when Android needs the memory they're using.

Since this is a standard feature, then changing the behavior to forcefully close would be changing the user experience. Many users would be used to the gentle dismissal of their Android apps so when they dismiss one with the intention of returning to it after performing some other tasks, they may be rather frustrated that the state of the application is reset, or that it takes longer to open. I would stick with the standard behavior because it is what is expected.

Andy E
+2  A: 

It's not possible. It's at the discretion of the operating system (Android) to decide when a process should be removed or remain in memory. This is for efficiency reasons: if the user decides to relaunch the app, then it's already there without it having to be loaded into memory.

So no, it's not only discouraged, it's impossible to do so.

Matthias
+3  A: 

Calling the finish() method on an Activity has your desired effect on that activity.

r1k0
No it doesn't. It finishes the current Activity, not the application. If you finish() the bottom-most Activity on the task stack, your application will appear to exit, but Android may decide to actually keep it around for as long as it sees fit.
Matthias
Indeed, however if you need to fully exit your application, you need to call the finish method for each activity and also think about any services you might have started. I've edited the initial answer also - sorry for the omission.
r1k0
+1  A: 

YES! You can most certainly close your application so it is no longer running in the background. Like others have commented "finish()" is the Google recommended way that doesn't really mean your program is closed.

System.exit(0);

That right there will close your application out leaving nothing running in the background. However, use this wisely and don't leave files open, database handles open, etc. These things would normally be cleaned up through the "finish()" command.

I personally HATE when I choose Exit in an application and it doesn't really exit.

pcm2a
Using System.exit() is absolutely not recommended.
CommonsWare
I won't argue that it's not the recommended way but can you please provide a solution that will guarantee that the application is exited from the background immediatly? If not then System.exit is the way to go until Google provides a better method.
pcm2a
@pcm2a: uhm, no. You're not *supposed* to. Resorting to hacks or dirty workarounds doesn't seem like a thing I would encourage.
Matthias
Who decides that you're not "supposed" to, the same people that created a method that doesn't actually exit? If users didn't want their applications closed then the 5th most popular paid app wouldn't be a task killer. People need memory freed up and the core OS doesn't do the job.
pcm2a
Agreed that it's ill-advised, but up-voted for providing an actual answer the the question asked. I'm getting very tired of hearing "you don't really want to do that", with no followup explanation. Android is an absolute nightmare regarding documentation for these types of things compared to iPhone.
DougW
A: 

But, if an application only has a single Activity, a call to finish() should stop the execution, right?

I am not sure because the documenation is somewhat confusing regarding applications, processes, threads, activities, etc. And besides, the life cycle diagram of an Activity is not a state diagram, which would have made more sense to me. I suspect that is because the guys in the Linux and Android world are not really any real real time programmers, and Linux is not a realtime opering system. And isn't it unusual to have applications without application code, just formhandlers, receivers and services?

Excuse me, but as an old realtime programer, it would have been nice to sometimes have more control of what is going on.

A: 

But, if an application only has a single Activity, a call to finish() should stop the execution, right?

I am not sure because the documenation is somewhat confusing regarding applications, processes, threads, activities, etc. And besides, the life cycle diagram of an Activity is not a state diagram, which would have made more sense to me. I suspect that is because the guys in the Linux and Android world are not really any real real time programmers, and Linux is not a realtime opering system. And isn't it unusual to have applications without application code, just formhandlers, receivers and services?

Excuse me, but as an old realtime programer, it would have been nice to sometimes have more control of what is going on.